Reputation: 61840
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)
}
}
application:openUrl
is implemented within AppDelegate
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
OAuthSwift.handle(url: url)
return true
}
using OAuthSwift library.
What am I doing wrong?
Permanently get the following:
Upvotes: 0
Views: 577
Reputation: 21259
Frankly speaking, based on your 48k rep, is this question some kind of attention test or ...? :)
Info.plist
says pl.blueworld.oauth2
, but your GitHub application callback URL is http://gitapp/github_oauth/callback
.
.
(gitapp
vs git.app
).rwURL
says pl.blueworld.oauth2:/oauth2Callback
.
/
(...oauth2:/oauth2...
vs ...oauth2://oauth2...
).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.
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
.
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)
}
}
}
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