Open camillegallet opened 4 years ago
I've also try with that class : https://github.com/p2/OAuth2/wiki/Alamofire-4
and created
let oauth2 = OAuth2CodeGrant(settings: [
"client_id": "1448b6616f2947919657805f8e8e46c2",
"client_secret": "6bc86e063ab9416b86d8e17a4b3d2727",
"authorize_uri": "https://accounts.spotify.com/en/authorize",
"token_uri": "https://accounts.spotify.com/api/token", // code grant only
"redirect_uris": ["kronos://oauth/callback"], // register your own "myapp" scheme in Info.plist
"scope": "user-library-modify",
"secret_in_body": true, // Github needs this
"keychain": false, // if you DON'T want keychain integration
] as OAuth2JSON)
//clientID: String, baseURLString: String, accessToken: String, refreshToken: String
let sessionManager = SessionManager()
let retrier = OAuth2RetryHandler(oauth2: oauth2)
sessionManager.adapter = retrier
sessionManager.retrier = retrier
self.alamofireManager = sessionManager // you must hold on to this somewhere
// Note that the `validate()` call here is important
sessionManager.request("https://api.spotify.com/v1/tracks/2KrxsD86ARO5beq7Q0Drfqa").validate().responseJSON { response in
debugPrint(response)
}
I've got "INVALID_CLIENT: Invalid redirect URI"
I've added that in the project info
The error was due to a missing config on the spotify's developper site But now, lets go back to my main issue: how to deal with OAuth without having the need to retrieve a JSON and directly get the token
Finaly I've found an answer to the problem now I do :
do{
let url = try appDelegate.oauth2!.authorizeURL(params:nil)
try appDelegate.oauth2!.authorizer.openAuthorizeURLInBrowser(url)
appDelegate.oauth2!.afterAuthorizeOrFail = { authParameters, error in
print("afterAuthorizeOrFail")
}
}catch let error {
DispatchQueue.main.async {
print("ERROR \(error)")
}
}
In addition I've that in the AppDelegate class
func application(_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
oauth2!.handleRedirectURL(url)
print(url)
return true
}
The function is called but not afterAuthorizeOrFail
Does someone know how to get the token?
I've found how to finally solve my problem:
do{
let url = try appDelegate.oauth2!.authorizeURL(params:nil)
appDelegate.oauth2!.afterAuthorizeOrFail = { authParameters, error in
print("afterAuthorizeOrFail")
}
try appDelegate.oauth2!.authorizer.openAuthorizeURLInBrowser(url)
}catch let error {
DispatchQueue.main.async {
print("ERROR \(error)")
}
}
Note that you need to swap afterAuthorizeOrFail and openAuthorizeURLInBrowser please fix the doc
Hello there, I want to get a token from an OAuth. For testing purpose I'm trying to do that with Spotify before working with my own company OAuth
But I don't understand well how to to the request to retrieve the token with the OAUth2 swift framework API
Thanks in advance for your help