Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61840

Result callback of Oauth2 for Github is not called

Why is my result callback not called at all when I am trying to authorize with oauth2 for Github?

Here is what I do when my view did appear:

    private func login() {
        let oauthswift = OAuth2Swift(
          consumerKey:    "1e7d92204b39361.....",
          consumerSecret: "6f32158240aca680537c19db010105a3bee.....",
          authorizeUrl:   "https://github.com/login/oauth/authorize",
          accessTokenUrl: "https://github.com/login/oauth/access_token",
          responseType:   "code"
        )
        oauthswift.allowMissingStateCheck = true
        oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift)
        guard let rwURL = URL(string: "pl.blueworld.oauth2:/oauth2Callback") else { return }
        oauthswift.authorize(withCallbackURL: rwURL, scope: "repo,gist", state: "") { result in
            print(result)
        }
    }

enter image description here

enter image description here

What am I doing wrong?

Permanently get the following:

enter image description here

Upvotes: 0

Views: 577

Answers (1)

zrzka
zrzka

Reputation: 21259

Frankly speaking, based on your 48k rep, is this question some kind of attention test or ...? :)

Authorization callback URL

enter image description here

  • Your Info.plist says pl.blueworld.oauth2, but your GitHub application callback URL is http://gitapp/github_oauth/callback.
    • Missing . (gitapp vs git.app).
  • Your rwURL says pl.blueworld.oauth2:/oauth2Callback.
    • Missing / (...oauth2:/oauth2... vs ...oauth2://oauth2...).
    • Weird case (C).

It differs in all those places.

If your intention is to use http://git.app/github_oauth/callback (with added .) and redirect to pl.blueworld.oauth2://oauth2Callback, fine, you can, but the rest still applies.

Info.plist

I've got dev-zrzka://oauth-callback (GH application callback URL). The scheme is dev-zrzka. Which is what you should use in the Info.plist.

enter image description here

Scene delegate

You're handling callback in the AppDelegate, but you should use SceneDelegate since iOS 13. It's mentioned even in the README. That's probably the reason why it isn't called (if we skip the gitapp vs git.app & redirection problem).

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?
    
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        guard let url = URLContexts.first?.url else {
            return
        }
        // GH Callback URL dev-zrzka://oauth-callback
        //                             ^^^^^^^^^^^^^^
        if url.host == "oauth-callback" {
            OAuthSwift.handle(url: url)
        }
    }
}

View controller

Next problem is that you have to store the OAuth2Swift instance somewhere otherwise you'll see failure(retain) (from the print(result)). All examples in the README says // create an instance and retain it.

import UIKit
import OAuthSwift

class ViewController: UIViewController {
    private var oauthswift: OAuth2Swift!
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        oauthswift = OAuth2Swift(
            consumerKey:    "...", // GH App Client ID
            consumerSecret: "...", // GH App Client Secret
            authorizeUrl:   "https://github.com/login/oauth/authorize",
            accessTokenUrl: "https://github.com/login/oauth/access_token",
            responseType:   "token"
        )
        oauthswift.allowMissingStateCheck = true
        oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift)
        guard let rwURL = URL(string: "dev-zrzka://oauth-callback") else { return }
        oauthswift.authorize(withCallbackURL: rwURL, scope: "repo,gist", state: "") { result in
            print(result)
        }
    }
}

Upvotes: 3

Related Questions