Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uber Authentication Fails with "Invalid Request Parameters" #88

Closed
sephethus opened this issue Aug 2, 2015 · 3 comments
Closed

Uber Authentication Fails with "Invalid Request Parameters" #88

sephethus opened this issue Aug 2, 2015 · 3 comments

Comments

@sephethus
Copy link

This code gets a response from Uber's site saying "Invalid Request Parameters". Something is wrong with the way the request is sent to Uber and I'm not sure what yet.

    let AUTHORIZE_URL = "https://login.uber.com/oauth/authorize"
    let CLIENT_KEY = "*****"
    let SERVER_TOKEN = "*****"
    let CLIENT_SECRET = "*****"
    let ACCESS_TOKEN_URL = "https://login.uber.com/oauth/token"
    let REDIRECT_URI = "MyAppName://oauth"

    func registerWithUber() {
        let params = [String: String]()
        let oauthUber = OAuth2Swift(consumerKey: CLIENT_KEY, consumerSecret: CLIENT_SECRET, authorizeUrl: AUTHORIZE_URL, responseType: "code")

        oauthUber.authorizeWithCallbackURL( NSURL(string: REDIRECT_URI)!, scope: "profile history", state: "", params: params, success: {
            credentials, response, parameters in
                println("Success")
                println(credentials.oauth_token) },

            failure: { (error: NSError) -> Void in
                println("Fail")
        })

    }

This is the string that gets sent to uber (api key replaced with *****)

https://login.uber.com/oauth/authorize?client_id=*****&redirect_uri=MyAppName://oauth&response_type=code&scope=profile%20history

Here is the authorizeWithCallbackURL method:

public func authorizeWithCallbackURL(callbackURL: NSURL, scope: String, state: String, params: Dictionary<String, String> = Dictionary<String, String>(), success: TokenSuccessHandler, failure: ((error: NSError) -> Void)) {
        self.observer = NSNotificationCenter.defaultCenter().addObserverForName(CallbackNotification.notificationName, object: nil, queue: NSOperationQueue.mainQueue(), usingBlock:{
            notification in
            NSNotificationCenter.defaultCenter().removeObserver(self.observer!)
            let url = notification.userInfo![CallbackNotification.optionsURLKey] as! NSURL
            var responseParameters: Dictionary<String, String> = Dictionary()
            if let query = url.query {
                //println("This happens too")
                responseParameters = query.parametersFromQueryString()
            }
            if ((url.fragment) != nil && url.fragment!.isEmpty == false) {
                responseParameters = url.fragment!.parametersFromQueryString()
            }
            if let accessToken = responseParameters["access_token"] {

                self.client.credential.oauth_token = accessToken
                success(credential: self.client.credential, response: nil, parameters: responseParameters)
            }
            if let code = responseParameters["code"] {

                self.postOAuthAccessTokenWithRequestTokenByCode(code.stringByRemovingPercentEncoding!,
                    callbackURL:callbackURL,
                    success: { credential, response, responseParameters in
                        success(credential: credential, response: response, parameters: responseParameters)
                }, failure: failure)

            }
        })

        //let authorizeURL = NSURL(string: )
        var urlString = String()
        urlString += self.authorize_url
        urlString += (self.authorize_url.has("?") ? "&" : "?") + "client_id=\(self.consumer_key)"
        urlString += "&redirect_uri=\(callbackURL.absoluteString!)"
        urlString += "&response_type=\(self.response_type)"
        if (scope != "") {
          urlString += "&scope=\(scope)"
        }
        if (state != "") {
            urlString += "&state=\(state)"
        }

        for param in params {
            urlString += "&\(param.0)=\(param.1)"
        }

        if let q = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
            println(q)
            if let queryURL = NSURL(string: q) {
                self.authorize_url_handler.handle(queryURL)
            }
        }
 }
@sephethus
Copy link
Author

I think I have solved the problem with Uber, this will require modification of OAuth2Swift.swift, just comment out the line that says:

urlString += "&redirect_uri=\(callbackURL.absoluteString!)"

Therefore:

var urlString = String()
        urlString += self.authorize_url
        urlString += (self.authorize_url.has("?") ? "&" : "?") + "client_id=\(self.consumer_key)"
        //urlString += "&redirect_uri=\(callbackURL.absoluteString!)"
        urlString += "&response_type=\(self.response_type)"

        if (scope != "") {
          urlString += "&scope=\(scope)"
        }

        if (state != "") {
            urlString += "&state=\(state)"
        }

        for param in params {
            urlString += "&\(param.0)=\(param.1)"
        }

        if let q = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {

            if let queryURL = NSURL(string: q) {
                self.authorize_url_handler.handle(queryURL)
            }
        }
}

Also, use the following URL for your callback:

https://oauthswift.herokuapp.com/callback/YourAppNameHere

I can see the code coming back to me when it returns me to my app, I'm using appdelegate openURL to run oauth2swift.handleopenurl(url). It crashes at this point, so on with the next problem.

@dongri
Copy link
Member

dongri commented Aug 26, 2015

Hi @sephethus

I Resolved Uber OAuth problem

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

Thanks!

@dongri
Copy link
Member

dongri commented Aug 26, 2015

@phimage phimage closed this as completed Nov 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants