[GH-ISSUE #454] Failing on handle(url: url) #297

Closed
opened 2026-03-03 16:47:28 +03:00 by kerem · 11 comments
Owner

Originally created by @TannerJuby1 on GitHub (Apr 16, 2018).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/454

Description:

All parts of the OAuth process is working as expected, except for the OAuthSwift.handle(url: url) piece in the app delegate on the open url function in the AppDelegate. I must be missing a piece of the set up that is not defined in the documentation.

   let oauthswift = OAuth2Swift(
        consumerKey: "**************",
        consumerSecret: "",
        authorizeUrl: "<authorize url>",
        accessTokenUrl: "",
        responseType: "code"
    )
    
    oauthswift.allowMissingStateCheck = true
    
    oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift)
                    
    // Authorize
    oauthswift.authorize(withCallbackURL: "com.devetry.ytp:", scope: "profile", state: "Custom", success: { (credential, response, params) in
        print("SUCCESS")
        print(credential)
    }) { (error) in
        print("ERROR: \(error.localizedDescription)")
    }

I am using the SafariURLHandler due to the easy handling of the web view you have in place, and have followed all the steps. I have the com.devetry.ytp URL Scheme set up in my Info.plist, and the app is able to successfully open the SafariWebView, authenticate, then close.

But in the AppDelegate, I have this piece of code:

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        print("Open Url: \(url)")
    
        OAuthSwift.handle(url: url)
    
        return true
    }

The url that is printed from my print() function is as expected, but the handle() function completes the authorize function in the failure block with a -11 error...

I assume my problem has something to do with my callback url (com.devetry.ytp:) but no matter where I look, I can't seem to find what I am missing. Can someone help me find out what going wrong?

Thanks!

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

custom oauth provider

OAuth Version:

  • Version 1
  • Version 2

OS (Please fill the version) :

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

Installation method:

  • Carthage
  • CocoaPods
  • Manually

Library version:

  • head
  • v1.2.1
  • v1.2 (Swift 4.0)
  • v1.0.0
  • v0.6
  • other: (Please fill in the version you are using.)

Xcode version:

  • 9.3 (Swift 4.1)

  • 9.0 (Swift 4.0)

  • 9.0 (Swift 3.2)

  • 8.x (Swift 3.x)

  • 8.0 (Swift 2.3)

  • 7.3.1

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

  • objective c

Originally created by @TannerJuby1 on GitHub (Apr 16, 2018). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/454 ### Description: All parts of the OAuth process is working as expected, except for the `OAuthSwift.handle(url: url)` piece in the app delegate on the `open url` function in the AppDelegate. I must be missing a piece of the set up that is not defined in the documentation. let oauthswift = OAuth2Swift( consumerKey: "**************", consumerSecret: "", authorizeUrl: "<authorize url>", accessTokenUrl: "", responseType: "code" ) oauthswift.allowMissingStateCheck = true oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift) // Authorize oauthswift.authorize(withCallbackURL: "com.devetry.ytp:", scope: "profile", state: "Custom", success: { (credential, response, params) in print("SUCCESS") print(credential) }) { (error) in print("ERROR: \(error.localizedDescription)") } I am using the ***SafariURLHandler*** due to the easy handling of the web view you have in place, and have followed all the steps. I have the `com.devetry.ytp` URL Scheme set up in my Info.plist, and the app is able to successfully open the SafariWebView, authenticate, then close. But in the AppDelegate, I have this piece of code: func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { print("Open Url: \(url)") OAuthSwift.handle(url: url) return true } The url that is printed from my `print()` function is as expected, but the `handle()` function completes the `authorize` function in the failure block with a -11 error... I assume my problem has something to do with my callback url (com.devetry.ytp:) but no matter where I look, I can't seem to find what I am missing. Can someone help me find out what going wrong? Thanks! ### OAuth Provider? (Twitter, Github, ..): custom oauth provider ### OAuth Version: - [ ] Version 1 - [x] Version 2 ### OS (Please fill the version) : - [x] iOS : - [ ] OSX : - [ ] TVOS : - [ ] WatchOS : ### Installation method: - [ ] Carthage - [x] CocoaPods - [ ] Manually ### Library version: - [ ] head - [ ] v1.2.1 - [x] v1.2 (Swift 4.0) - [ ] v1.0.0 - [ ] v0.6 - [ ] other: (Please fill in the version you are using.) ### Xcode version: - [x] 9.3 (Swift 4.1) - [ ] 9.0 (Swift 4.0) - [ ] 9.0 (Swift 3.2) - [ ] 8.x (Swift 3.x) - [ ] 8.0 (Swift 2.3) - [ ] 7.3.1 - [ ] other: (Please fill in the version you are using.) - [ ] objective c
kerem closed this issue 2026-03-03 16:47:29 +03:00
Author
Owner

@phimage commented on GitHub (Apr 16, 2018):

-11, not -10?

You must retain your oauthswift object. As class attribute for instance

Then no more detail about the error? Using Xcode debug

<!-- gh-comment-id:381674156 --> @phimage commented on GitHub (Apr 16, 2018): -11, not -10? You must retain your oauthswift object. As class attribute for instance Then no more detail about the error? Using Xcode debug
Author
Owner

@TannerJuby1 commented on GitHub (Apr 16, 2018):

Sorry for the lack of vocabulary, but what do you mean by retain my OAuthSwift object?

And yes, I get a -11 error, not a -10. The OAuthSwiftError.response is a 403 HTTP error, so forbidden. But that makes zero sense to me, I've already handled the authorization, and it succeeded and sent me back a code in the url that the handle() function is attempting to handle

<!-- gh-comment-id:381679155 --> @TannerJuby1 commented on GitHub (Apr 16, 2018): Sorry for the lack of vocabulary, but what do you mean by retain my OAuthSwift object? And yes, I get a -11 error, not a -10. The OAuthSwiftError.response is a 403 HTTP error, so forbidden. But that makes zero sense to me, I've already handled the authorization, and it succeeded and sent me back a code in the url that the `handle()` function is attempting to handle
Author
Owner

@TannerJuby1 commented on GitHub (Apr 16, 2018):

However, if I change my callback url to this 'com.devetry.ytp://host/_oauth/drupal?close' (and update my client to accept this redirect url). And host is the IP address for the client, I just don't want to provide it to the public.

I get a -5 error with the description: serverError[No access_token, no code and no error provided by server]

<!-- gh-comment-id:381682446 --> @TannerJuby1 commented on GitHub (Apr 16, 2018): However, if I change my callback url to this 'com.devetry.ytp://host/_oauth/drupal?close' (and update my client to accept this redirect url). And `host` is the IP address for the client, I just don't want to provide it to the public. I get a -5 error with the description: `serverError[No access_token, no code and no error provided by server]`
Author
Owner

@phimage commented on GitHub (Apr 16, 2018):

could you post here the url receive passed to OAuthSwift.handle(

<!-- gh-comment-id:381705219 --> @phimage commented on GitHub (Apr 16, 2018): could you post here the url receive passed to OAuthSwift.handle(
Author
Owner

@TannerJuby1 commented on GitHub (Apr 16, 2018):

Yeah of course. So with the callback url set to 'com.devetry.ytp://host/_oauth/drupal?close', the url that is being passed to the OAuth.handle() is:

com.devetry.ytp://host/_oauth/drupal?close&code=the_code&state=the_state

and again, host is actually an IP Address, and the_code is the code that's passes back, and the_state is the state thats passed back

<!-- gh-comment-id:381709888 --> @TannerJuby1 commented on GitHub (Apr 16, 2018): Yeah of course. So with the callback url set to 'com.devetry.ytp://host/_oauth/drupal?close', the url that is being passed to the OAuth.handle() is: `com.devetry.ytp://host/_oauth/drupal?close&code=the_code&state=the_state` and again, `host` is actually an IP Address, and `the_code` is the code that's passes back, and `the_state` is the state thats passed back
Author
Owner

@phimage commented on GitHub (Apr 16, 2018):

Strange, if the code is in query...the serverError must not be thrown

<!-- gh-comment-id:381710644 --> @phimage commented on GitHub (Apr 16, 2018): Strange, if the code is in query...the serverError must not be thrown
Author
Owner

@TannerJuby1 commented on GitHub (Apr 16, 2018):

I am getting back the serverError: [No access_token, no code and no error provided by server]

<!-- gh-comment-id:381716705 --> @TannerJuby1 commented on GitHub (Apr 16, 2018): I am getting back the serverError: [No access_token, no code and no error provided by server]
Author
Owner

@phimage commented on GitHub (Apr 17, 2018):

Difficult to help without debugging it

In OAuthSwift2.swift
there is open func authorize(withCallbackURL callbackURL: URL, scope: String, state: String, parameters: Parameters = [:], headers: OAuthSwift.Headers? = nil, success: @escaping TokenSuccessHandler, failure: FailureHandler?) -> OAuthSwiftRequestHandle? {

The code transform url to dictionary

if let query = url.query {
    responseParameters += query.parametersFromQueryString
}

Then your error must not be thrown if there is code inside query

if let code = responseParameters["code"]

So with debug we must look at the content of responseParameters

Maybe the "close" make an issue

<!-- gh-comment-id:381876982 --> @phimage commented on GitHub (Apr 17, 2018): Difficult to help without debugging it In OAuthSwift2.swift there is `open func authorize(withCallbackURL callbackURL: URL, scope: String, state: String, parameters: Parameters = [:], headers: OAuthSwift.Headers? = nil, success: @escaping TokenSuccessHandler, failure: FailureHandler?) -> OAuthSwiftRequestHandle? {` The code transform url to dictionary ```swift if let query = url.query { responseParameters += query.parametersFromQueryString } ``` Then your error must not be thrown if there is code inside query ```swift if let code = responseParameters["code"] ``` So with debug we must look at the content of `responseParameters` Maybe the "close" make an issue
Author
Owner

@phimage commented on GitHub (Apr 17, 2018):

I make a unit test and see the error
The "close" make the failure
close&toto=1&code= will work or close=1&code=
I will fix

<!-- gh-comment-id:381881297 --> @phimage commented on GitHub (Apr 17, 2018): I make a unit test and see the error The "close" make the failure close&toto=1&code= will work or close=1&code= I will fix
Author
Owner

@TannerJuby1 commented on GitHub (Apr 17, 2018):

Thanks for the quick update!

After changing my callback url to com.devetry.ytp://host/_oauth/drupal?close=1 I am now getting a retain (-11) error. I remember you said something about I need to retain my oauthswift object, as a class attribute, so I assume that's what I need to do.. But what do you mean by this? Can you point me to an example?

Thanks!

<!-- gh-comment-id:382058447 --> @TannerJuby1 commented on GitHub (Apr 17, 2018): Thanks for the quick update! After changing my callback url to `com.devetry.ytp://host/_oauth/drupal?close=1` I am now getting a `retain (-11)` error. I remember you said something about I need to retain my oauthswift object, as a class attribute, so I assume that's what I need to do.. But what do you mean by this? Can you point me to an example? Thanks!
Author
Owner

@phimage commented on GitHub (Apr 17, 2018):

See demo app

Let oauthswift = is forbidden
Do
Self.oauthswift = ...

<!-- gh-comment-id:382067412 --> @phimage commented on GitHub (Apr 17, 2018): See demo app Let oauthswift = is forbidden Do Self.oauthswift = ...
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#297
No description provided.