[GH-ISSUE #592] Implement another OAuth website #385

Closed
opened 2026-03-03 16:48:15 +03:00 by kerem · 11 comments
Owner

Originally created by @camillegallet on GitHub (May 2, 2020).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/592

Description:

I'm trying to add another OAuth website
This not a bug

OAuth Provider? (Twitter, Github, ..):

OAuth Version:

  • Version 1
  • Version 2

OS (Please fill the version) :

  • iOS :
  • OSX :
  • TVOS :
  • WatchOS :

Installation method:

  • Carthage
  • CocoaPods
  • Swift Package Manager
  • Manually

Library version:

  • head
  • v2.1.0
  • v2.0.0
  • v1.4.1
  • v1.2 (Swift 4.0)
  • other: (Please fill in the version you are using.)

Xcode version:

  • 11.x (Swift 5.1)

  • 10.x (Swift 5.0)

  • 10.x (Swift 4.1)

  • 9.3 (Swift 4.1)

  • 9.0 (Swift 4.0)

  • other: (Please fill in the version you are using.)

  • objective c

I'm trying OAuthSwift
I've successfully imported the demo project and I try to add another OAuth identification. So before testing to add my company's OAuth to begin I've added this in the ViewController

func doOAuthTest(_ serviceParameters: [String:String]){
    let oauthswift = OAuth2Swift(
        consumerKey:    "YbCkiZe3-uAKj-XeugzG1WTu",
        consumerSecret: "_P68mzofmEDhNZHu7LXFGUJWAXcML98VYSuVRv29NFUSAa01",
        authorizeUrl:   "https://authorization-server.com/authorize",
        responseType:   "code"
    )

    self.oauthswift = oauthswift
    oauthswift.encodeCallbackURL = true
    oauthswift.encodeCallbackURLQuery = false
    oauthswift.authorizeURLHandler = getURLHandler()
    
    let redirectURL = "https://www.oauth.com/playground/authorization-code.html"
    
    let state = "ej5m6zmYn5GAPiXM"
    let _ = oauthswift.authorize(
    withCallbackURL: URL(string: redirectURL)!,scope: "", state:state) { result in
        switch result {
        case .success(let (credential, _, _)):
            print("success")
            self.showTokenAlert(name: serviceParameters["name"], credential: credential)
        case .failure(let error):
            print("failure")
            print(error.description)
        }
    }
}

Secondly, I've edited the ViewDidLoad like that:

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Load config from files
    initConf()
    let _ = internalWebViewController.webView
    let param = ["consumerSecret": "", "name": "Test", "consumerKey": ""]
    doOAuthTest(param)
}

And finally

func getURLHandler() -> OAuthSwiftURLHandlerType {
        if internalWebViewController.parent == nil {
            self.addChild(internalWebViewController)
            navigationController?.setNavigationBarHidden(true, animated: false)
        }
        return internalWebViewController
}

I don't know why I've neither success or failure in the console
What I've done wrong?
Thanks in advance

Originally created by @camillegallet on GitHub (May 2, 2020). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/592 ### Description: I'm trying to add another OAuth website This not a bug ### OAuth Provider? (Twitter, Github, ..): ### OAuth Version: - [ ] Version 1 - [x] Version 2 ### OS (Please fill the version) : - [x] iOS : - [ ] OSX : - [ ] TVOS : - [ ] WatchOS : ### Installation method: - [ ] Carthage - [ ] CocoaPods - [ ] Swift Package Manager - [x] Manually ### Library version: - [x] head - [ ] v2.1.0 - [ ] v2.0.0 - [ ] v1.4.1 - [ ] v1.2 (Swift 4.0) - [ ] other: (Please fill in the version you are using.) ### Xcode version: - [x] 11.x (Swift 5.1) - [ ] 10.x (Swift 5.0) - [ ] 10.x (Swift 4.1) - [ ] 9.3 (Swift 4.1) - [ ] 9.0 (Swift 4.0) - [ ] other: (Please fill in the version you are using.) - [ ] objective c I'm trying OAuthSwift I've successfully imported the demo project and I try to add another OAuth identification. So before testing to add my company's OAuth to begin I've added this in the ViewController func doOAuthTest(_ serviceParameters: [String:String]){ let oauthswift = OAuth2Swift( consumerKey: "YbCkiZe3-uAKj-XeugzG1WTu", consumerSecret: "_P68mzofmEDhNZHu7LXFGUJWAXcML98VYSuVRv29NFUSAa01", authorizeUrl: "https://authorization-server.com/authorize", responseType: "code" ) self.oauthswift = oauthswift oauthswift.encodeCallbackURL = true oauthswift.encodeCallbackURLQuery = false oauthswift.authorizeURLHandler = getURLHandler() let redirectURL = "https://www.oauth.com/playground/authorization-code.html" let state = "ej5m6zmYn5GAPiXM" let _ = oauthswift.authorize( withCallbackURL: URL(string: redirectURL)!,scope: "", state:state) { result in switch result { case .success(let (credential, _, _)): print("success") self.showTokenAlert(name: serviceParameters["name"], credential: credential) case .failure(let error): print("failure") print(error.description) } } } Secondly, I've edited the ViewDidLoad like that: override func viewDidLoad() { super.viewDidLoad() // Load config from files initConf() let _ = internalWebViewController.webView let param = ["consumerSecret": "", "name": "Test", "consumerKey": ""] doOAuthTest(param) } And finally func getURLHandler() -> OAuthSwiftURLHandlerType { if internalWebViewController.parent == nil { self.addChild(internalWebViewController) navigationController?.setNavigationBarHidden(true, animated: false) } return internalWebViewController } I don't know why I've neither success or failure in the console What I've done wrong? Thanks in advance
kerem closed this issue 2026-03-03 16:48:16 +03:00
Author
Owner

@mesopelagique commented on GitHub (May 3, 2020):

AppDelegate code ?
The redirection url is important, this url is used to transmit the token of authentication
In readme there is instructions
For instance using special scheme like my app://callback or by getting web view event
OAuthSwift.handle must be called (with the callback url and token added as query parameter)

<!-- gh-comment-id:623077932 --> @mesopelagique commented on GitHub (May 3, 2020): AppDelegate code ? The redirection url is important, this url is used to transmit the token of authentication In readme there is instructions For instance using special scheme like my app://callback or by getting web view event OAuthSwift.handle must be called (with the callback url and token added as query parameter)
Author
Owner

@camillegallet commented on GitHub (May 3, 2020):

Thanks for your reply
My app delegated class is that:
https://pastebin.com/VZmNGdAN

Also If I add a print like that

  func applicationHandle(url: URL) {
    print("app Handle")
    if (url.host == "oauth-callback") {
        OAuthSwift.handle(url: url)
    } else {
        // Google provider is the only one with your.bundle.id url schema.
        OAuthSwift.handle(url: url)
    }
}

I don't see it in the console

<!-- gh-comment-id:623078347 --> @camillegallet commented on GitHub (May 3, 2020): Thanks for your reply My app delegated class is that: [https://pastebin.com/VZmNGdAN](https://pastebin.com/VZmNGdAN) Also If I add a print like that func applicationHandle(url: URL) { print("app Handle") if (url.host == "oauth-callback") { OAuthSwift.handle(url: url) } else { // Google provider is the only one with your.bundle.id url schema. OAuthSwift.handle(url: url) } } I don't see it in the console
Author
Owner

@camillegallet commented on GitHub (May 3, 2020):

Just in case I also post the code of Webviewcontroller
https://pastebin.com/Q5grL5qS

<!-- gh-comment-id:623080358 --> @camillegallet commented on GitHub (May 3, 2020): Just in case I also post the code of Webviewcontroller https://pastebin.com/Q5grL5qS
Author
Owner

@camillegallet commented on GitHub (May 3, 2020):

Some new, I've open the OAuthSwift.xcodeproj, launched it and clicked on Spotify.
In parrallel I've generated a pair Client ID/Client Secret on spotify
I've edited the doOAuthSpotify to print debugging messages

 func doOAuthSpotify(_ serviceParameters: [String:String]){
    let oauthswift = OAuth2Swift(
        consumerKey:    serviceParameters["consumerKey"]!,
        consumerSecret: serviceParameters["consumerSecret"]!,
        authorizeUrl:   "https://accounts.spotify.com/en/authorize",
        accessTokenUrl: "https://accounts.spotify.com/api/token",
        responseType:   "code"
    )
    self.oauthswift = oauthswift
    oauthswift.authorizeURLHandler = getURLHandler()
    let state = generateState(withLength: 20)
    
    let _ = oauthswift.authorize(
        withCallbackURL: URL(string: "oauth-swift://oauth-callback/spotify")!,
        scope: "user-library-modify",
        state: state) { result in
            switch result {
            case .success(let (credential, _, _)):
                print("success")
                self.showTokenAlert(name: serviceParameters["name"], credential: credential)
            case .failure(let error):
                print("fail")
                print(error.description)
            }
    }
}

But here is what I've got in the console :
https://pastebin.com/9cYNzPAP

<!-- gh-comment-id:623116431 --> @camillegallet commented on GitHub (May 3, 2020): Some new, I've open the OAuthSwift.xcodeproj, launched it and clicked on Spotify. In parrallel I've generated a pair Client ID/Client Secret on spotify I've edited the doOAuthSpotify to print debugging messages func doOAuthSpotify(_ serviceParameters: [String:String]){ let oauthswift = OAuth2Swift( consumerKey: serviceParameters["consumerKey"]!, consumerSecret: serviceParameters["consumerSecret"]!, authorizeUrl: "https://accounts.spotify.com/en/authorize", accessTokenUrl: "https://accounts.spotify.com/api/token", responseType: "code" ) self.oauthswift = oauthswift oauthswift.authorizeURLHandler = getURLHandler() let state = generateState(withLength: 20) let _ = oauthswift.authorize( withCallbackURL: URL(string: "oauth-swift://oauth-callback/spotify")!, scope: "user-library-modify", state: state) { result in switch result { case .success(let (credential, _, _)): print("success") self.showTokenAlert(name: serviceParameters["name"], credential: credential) case .failure(let error): print("fail") print(error.description) } } } But here is what I've got in the console : https://pastebin.com/9cYNzPAP
Author
Owner

@phimage commented on GitHub (May 4, 2020):

for this last issue, it's a demo issue. The main thread checker is activated, some code are executed in other thread on User Interace but to not have some graphical bugs, it must be in main thread.
To fix a DispatchQueue.main.async { the code }

<!-- gh-comment-id:623315383 --> @phimage commented on GitHub (May 4, 2020): for this last issue, it's a demo issue. The main thread checker is activated, some code are executed in other thread on User Interace but to not have some graphical bugs, it must be in main thread. To fix a `DispatchQueue.main.async { the code }`
Author
Owner

@phimage commented on GitHub (May 4, 2020):

It's difficult to follow with peace of code. If you create a project on GitHub with no client/consumer id and secret I will study and look for missing code

The important part is the callback url.
If you use one with MyApp:// (like oauthswift:// but you must use your own app name) you must configure your app https://github.com/OAuthSwift/OAuthSwift#setting-url-schemes
If you use https:// url, you must use delegate of web view to get the url and pass it to oauthswift, or be able to add special file on server to redirect http call to your app

<!-- gh-comment-id:623320134 --> @phimage commented on GitHub (May 4, 2020): It's difficult to follow with peace of code. If you create a project on GitHub with no client/consumer id and secret I will study and look for missing code The important part is the callback url. If you use one with MyApp:// (like oauthswift:// but you must use your own app name) you must configure your app https://github.com/OAuthSwift/OAuthSwift#setting-url-schemes If you use https:// url, you must use delegate of web view to get the url and pass it to oauthswift, or be able to add special file on server to redirect http call to your app
Author
Owner

@camillegallet commented on GitHub (May 4, 2020):

Here is a copy of my project https://github.com/camillegallet/testOAuth

<!-- gh-comment-id:623405196 --> @camillegallet commented on GitHub (May 4, 2020): Here is a copy of my project https://github.com/camillegallet/testOAuth
Author
Owner

@phimage commented on GitHub (May 4, 2020):

basically you try to copy the api demo, this app show all provider/services
and multiple methods, and user interface to select service.
maybe the code is too complicated, compared to a simple app with only one service

I could help only if I now where you want to connect, and the service used, ie. link to oauth api documentation, because the code do not show it
So the mobi.kronos-sport.com is based on? wordpress? others?


I see that too

https://stackoverflow.com/questions/61528076/how-can-i-save-credentials-session-in-a-ios-web-app

sensible information on iOS must be stored in Keychain
less sensible could be in userDefaults, file, etc..
I make a project prephirences to manage that but you can find many to store in keychain (awesome swift list done that)

<!-- gh-comment-id:623436026 --> @phimage commented on GitHub (May 4, 2020): basically you try to copy the api demo, this app show all provider/services and multiple methods, and user interface to select service. maybe the code is too complicated, compared to a simple app with only one service I could help only if I now where you want to connect, and the service used, ie. link to oauth api documentation, because the code do not show it So the mobi.kronos-sport.com is based on? wordpress? others? --- I see that too https://stackoverflow.com/questions/61528076/how-can-i-save-credentials-session-in-a-ios-web-app sensible information on iOS must be stored in Keychain less sensible could be in userDefaults, file, etc.. I make a project prephirences to manage that but you can find many to store in keychain (awesome swift list done that)
Author
Owner

@camillegallet commented on GitHub (May 4, 2020):

For the moment my company is developping an custom homemade OAuth service, that's why I'm trying with spotify (I've logged into developer.spotify.com and register an app to get a pair Client ID/Client Secret)
Yes now I use keychain

<!-- gh-comment-id:623438671 --> @camillegallet commented on GitHub (May 4, 2020): For the moment my company is developping an custom homemade OAuth service, that's why I'm trying with spotify (I've logged into developer.spotify.com and register an app to get a pair Client ID/Client Secret) Yes now I use keychain
Author
Owner

@phimage commented on GitHub (May 4, 2020):

ok I could show a simple Spotify app

<!-- gh-comment-id:623440277 --> @phimage commented on GitHub (May 4, 2020): ok I could show a simple Spotify app
Author
Owner

@phimage commented on GitHub (May 4, 2020):

I close, I cannot offer more support without any issue or real server information

<!-- gh-comment-id:623464007 --> @phimage commented on GitHub (May 4, 2020): I close, I cannot offer more support without any issue or real server information
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#385
No description provided.