0

I want to login to Facebook and redirect to my app but I have a problem. There is my code:

 let oauthswift = OAuth2Swift(
                consumerKey:    "4242424242",
                consumerSecret: "",
                authorizeUrl:   "https://www.facebook.com/dialog/oauth",
                accessTokenUrl: "https://graph.facebook.com/oauth/access_token",
                responseType:   "code"
            )
oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift)
          let state = generateState(withLength: 20)
          guard let rwURL = URL(string:"https://myapp.test/callback/facebook") else { return }
         let _ = oauthswift.authorize( withCallbackURL:rwURL, scope: "public_profile", state: state,
                success: { credential, response, parameters in
                  ..........}

Bundle is ID is myapp.test

Info.plist:

 <key>CFBundleURLTypes</key>
        <array>
            <dict>
                <key>CFBundleURLSchemes</key>
                <array>
                    <string>myapp.test</string>
                </array>
            </dict>
        </array>

When I try to redirect into my app "Safari cannot open the page because the server cannot be found"

In developers.facebook.com page in the section "Valid OAuth redirect URIs" I have same url "https://myapp.test/callback/facebook". What I am doing wrong? Thank you.

1 Answers1

1

If you want OAuth to open your app after the authentication, the redirect URL should be myapp.test://callback/facebook, not https://.... Otherwise it will simply try to open this URL inside safari.

Let me know if that helps.

Julien Perrenoud
  • 1,401
  • 11
  • 20
  • in this case my redirect url in safari is: (https://www.facebook.com/dialog/oauth?client_id=42424242&redirect_uri=myapp.test://callback/facebook&response_type=code&scope=public_profile&state=GT2k9nza7XBleUhQ8RNk) and error is : the redirect url must be absolute – alpha corrado Mar 20 '18 at 09:01
  • Try the following: `myapp://oauth-callback` (and don't forget to add `myapp` as URL scheme). When I remove the dot in the first part or your URI it switches the error to `The redirect_uri URL is not supported` so it might be the reason why it does not work for you. Also this thread might help you: https://stackoverflow.com/questions/33788368/how-to-setup-the-callback-url-in-the-oauthswift-library#33813008 – Julien Perrenoud Mar 20 '18 at 17:42