[GH-ISSUE #95] Uber Authentication Fails with HTTP Status 401: Unauthorized, Response: {"error": "invalid_client"} #63

Closed
opened 2026-03-03 16:45:19 +03:00 by kerem · 7 comments
Owner

Originally created by @sephethus on GitHub (Aug 24, 2015).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/95

OAuthSwift is incompatible with Uber's API and here is why. See my code below and the explanation for why following.

func registerWithUber() {
        let params = [String: String]()
        let oauthUber = OAuth2Swift(consumerKey: CLIENT_KEY, consumerSecret: CLIENT_SECRET, authorizeUrl: AUTHORIZE_URL, accessTokenUrl: ACCESS_TOKEN_URL, responseType: "code")
        oauthUber.authorize_url_handler = WebViewController()
        oauthUber.authorizeWithCallbackURL( NSURL(string: REDIRECT_URI)!, scope: "profile", state: "", params: params, success: {
            credentials, response, parameters in
                println("We have the key here: \(credentials.oauth_token)")
                NSUserDefaults.standardUserDefaults().setObject(credentials.oauth_token, forKey: "uberToken")
                NSUserDefaults.standardUserDefaults().synchronize()
            },

            failure: { (error: NSError) -> Void in
                println("Failed: \(error.localizedDescription)")
        })

}

See step three, get an Access token: https://developer.uber.com/v1/auth/

This is where it fails.

I traced this all the way to the NSURLConnection point and beyond this I have no way to figure out what it's doing or what's not being sent or included. The client ID most definitely makes it all the way to the end. I found that the headers for me look like this:

Value: OAuth oauth_consumer_key="", oauth_nonce="99F7307C", oauth_signature="HPM9Yfsa54maDehZYJsIDcUQLwY%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1440433137", oauth_version="1.0"

It's supposed to be called "client ID not oauth_consumer_key so this may or may not be the issue.

One thing is for certain, the problem is fixed by commenting out the addition of headers inside the makeRequest() method in OAuthSwiftHTTPRequest.swift

for (key, value) in headers {
                request.setValue(value, forHTTPHeaderField: key)
}

These headers are needed for other API's however, and I am connecting with other API's as well. So this is not a good solution, but rather a big problem. Some code may need to be added somewhere in order to determine whether this is an Uber request or not.

Originally created by @sephethus on GitHub (Aug 24, 2015). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/95 OAuthSwift is incompatible with Uber's API and here is why. See my code below and the explanation for why following. ``` func registerWithUber() { let params = [String: String]() let oauthUber = OAuth2Swift(consumerKey: CLIENT_KEY, consumerSecret: CLIENT_SECRET, authorizeUrl: AUTHORIZE_URL, accessTokenUrl: ACCESS_TOKEN_URL, responseType: "code") oauthUber.authorize_url_handler = WebViewController() oauthUber.authorizeWithCallbackURL( NSURL(string: REDIRECT_URI)!, scope: "profile", state: "", params: params, success: { credentials, response, parameters in println("We have the key here: \(credentials.oauth_token)") NSUserDefaults.standardUserDefaults().setObject(credentials.oauth_token, forKey: "uberToken") NSUserDefaults.standardUserDefaults().synchronize() }, failure: { (error: NSError) -> Void in println("Failed: \(error.localizedDescription)") }) } ``` See step three, get an Access token: https://developer.uber.com/v1/auth/ This is where it fails. I traced this all the way to the NSURLConnection point and beyond this I have no way to figure out what it's doing or what's not being sent or included. The client ID most definitely makes it all the way to the end. I found that the headers for me look like this: Value: OAuth oauth_consumer_key="<my proper clientID is here>", oauth_nonce="99F7307C", oauth_signature="HPM9Yfsa54maDehZYJsIDcUQLwY%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1440433137", oauth_version="1.0" It's supposed to be called "client ID not oauth_consumer_key so this may or may not be the issue. One thing is for certain, the problem is fixed by commenting out the addition of headers inside the makeRequest() method in OAuthSwiftHTTPRequest.swift ``` for (key, value) in headers { request.setValue(value, forHTTPHeaderField: key) } ``` These headers are needed for other API's however, and I am connecting with other API's as well. So this is not a good solution, but rather a big problem. Some code may need to be added somewhere in order to determine whether this is an Uber request or not.
kerem closed this issue 2026-03-03 16:45:19 +03:00
Author
Owner

@sephethus commented on GitHub (Aug 24, 2015):

Temporary fix by replacing code in makeRequest method inside OAuthSwiftHTTPRequest.swift with the following:

if request.URL?.host == "login.uber.com" {
          print("Uber request: Skipping headers\n")
} else {
          print("Adding Headers\n")
          for (key, value) in headers {
              request.setValue(value, forHTTPHeaderField: key)
          }
}
<!-- gh-comment-id:134298509 --> @sephethus commented on GitHub (Aug 24, 2015): Temporary fix by replacing code in makeRequest method inside OAuthSwiftHTTPRequest.swift with the following: ``` if request.URL?.host == "login.uber.com" { print("Uber request: Skipping headers\n") } else { print("Adding Headers\n") for (key, value) in headers { request.setValue(value, forHTTPHeaderField: key) } } ```
Author
Owner

@phimage commented on GitHub (Aug 25, 2015):

headers are mandatory to connect
disable it here is not the solution

a better place is request method of OAuthSwiftClient class (also multiPartRequest, code must be factorized here)
this is where headers are created
And don't put "uber" string into your api, think about a generic solution like adding a Set of string in OAuthSwiftClient named noHeaderHost

But I think there is another problem
Your headers seem to be oauth1, maybe same issue https://github.com/dongri/OAuthSwift/issues/94
I will add code to this issue that you can test, and then in two day make a PR...

<!-- gh-comment-id:134471987 --> @phimage commented on GitHub (Aug 25, 2015): headers are mandatory to connect disable it here is not the solution a better place is request method of OAuthSwiftClient class (also multiPartRequest, code must be factorized here) this is where headers are created And don't put "uber" string into your api, think about a generic solution like adding a Set of string in OAuthSwiftClient named noHeaderHost But I think there is another problem Your headers seem to be oauth1, maybe same issue https://github.com/dongri/OAuthSwift/issues/94 I will add code to this issue that you can test, and then in two day make a PR...
Author
Owner

@sephethus commented on GitHub (Aug 25, 2015):

No, they aren't mandatory to connect apparently, because I get a token and it authenticates ONLY if I take the headers out. My headers are OAuth2. I'm using OAuth2Swift as you can see above. Plus, as you said, postOAuthAccessTokenWithRequestTokenByCode gets called, therefore OAuth2 is set to true.

<!-- gh-comment-id:134474276 --> @sephethus commented on GitHub (Aug 25, 2015): No, they aren't mandatory to connect apparently, because I get a token and it authenticates ONLY if I take the headers out. My headers are OAuth2. I'm using OAuth2Swift as you can see above. Plus, as you said, postOAuthAccessTokenWithRequestTokenByCode gets called, therefore OAuth2 is set to true.
Author
Owner

@phimage commented on GitHub (Aug 25, 2015):

No your header is not OAuth2....postOAuthAccessTokenWithRequestTokenByCode has not been called
Just read your header : oauth_version="1.0"
Don't mix multiple try
And the uber return "access_token" not "code" so same as #94

Then don't mix header and query parameters
There is client id in query parameters

Then see "STEP FOUR: USE BEARER TOKEN" in uber doc, header is used to authentificate request

<!-- gh-comment-id:134505561 --> @phimage commented on GitHub (Aug 25, 2015): No your header is not OAuth2....postOAuthAccessTokenWithRequestTokenByCode has not been called Just read your header : oauth_version="1.0" Don't mix multiple try And the uber return "access_token" not "code" so same as #94 Then don't mix header and query parameters There is client id in query parameters Then see "STEP FOUR: USE BEARER TOKEN" in uber doc, header is used to authentificate request
Author
Owner

@sephethus commented on GitHub (Aug 25, 2015):

Why is that happening? I'm using OAuth2Swift when creating the object, not OAuth1Swift.

I also can put println("postOAuthAccessTokenWithRequestTokenByCode is being called") inside of that method and it prints to the debug console.

let oauthUber = OAuth2Swift(consumerKey: CLIENT_KEY, consumerSecret: CLIENT_SECRET, authorizeUrl: AUTHORIZE_URL, accessTokenUrl: ACCESS_TOKEN_URL, responseType: "code")
<!-- gh-comment-id:134576161 --> @sephethus commented on GitHub (Aug 25, 2015): Why is that happening? I'm using OAuth2Swift when creating the object, not OAuth1Swift. I also can put `println("postOAuthAccessTokenWithRequestTokenByCode is being called")` inside of that method and it prints to the debug console. ``` let oauthUber = OAuth2Swift(consumerKey: CLIENT_KEY, consumerSecret: CLIENT_SECRET, authorizeUrl: AUTHORIZE_URL, accessTokenUrl: ACCESS_TOKEN_URL, responseType: "code") ```
Author
Owner

@dongri commented on GitHub (Aug 26, 2015):

Hi @sephethus @phimage

I Resolved Uber OAuth problem

https://github.com/dongri/OAuthSwift/pull/96/files

Thanks!

<!-- gh-comment-id:134894009 --> @dongri commented on GitHub (Aug 26, 2015): Hi @sephethus @phimage I Resolved Uber OAuth problem https://github.com/dongri/OAuthSwift/pull/96/files Thanks!
Author
Owner

@dongri commented on GitHub (Aug 26, 2015):

Uber Wiki
https://github.com/dongri/OAuthSwift/wiki/Uber

<!-- gh-comment-id:134895246 --> @dongri commented on GitHub (Aug 26, 2015): Uber Wiki https://github.com/dongri/OAuthSwift/wiki/Uber
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#63
No description provided.