I am using the Uber request endpoint in my iOS application (Swift). The request API / endpoint requires user authentication using the application, here is the doc .
For this I use this Oauth2.0 library
What i did is
successfully integrated the library into my project (xCode) using these installation instructions.
In My AppDelegate
let uber_OAuth_Settings = [ "client_id": "XXXXXXX9vtKzobvXXXXXX", "client_secret": "EXXXXXXXXyFUNCa_Wez6AXXXXXXXnrXtxus", "authorize_uri": "https://login.uber.com/oauth/authorize", "token_uri": "https://login.uber.com/oauth/token", "redirect_uris": ["jamesappv2://oauth/callback"],
var oauth: OAuth2CodeGrant!
in my didFinishLaunchingWithOptions method of the didFinishLaunchingWithOptions application
oauth = OAuth2CodeGrant(settings: uber_OAuth_Settings) oauth.viewTitle = "Uber Login Service" // optional oauth.verbose = true // For Logs
Remember to register the URL scheme ie ( "redirect_uris": ["jamesappv2://oauth/callback"] )
go to your application. Target â info Tab â Url Types â Click (+), attached image

In AppDelegate, add the method below that handles the callback URL
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool { // you should probably first check if this is your URL being opened var splitUrl = url.absoluteString!.componentsSeparatedByString(":") if splitUrl[0] == ("jamesappv2") { oauth.handleRedirectURL(url) } return true }
Now in my viewController I liked this on myBtnClick
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let url = appDelegate.oauth.authorizeURL() UIApplication.sharedApplication().openURL(url) appDelegate.oauth.onAuthorize = { parameters in println("Did authorize with parameters: \(parameters)") self.navigationController?.pushViewController(self.PersonalDriverUber_VC, animated: true) //On Authorization Goto another ViewController using pushViewController of navigationcontroller Method } appDelegate.oauth.onFailure = { error in // `error` is nil on cancel if nil != error { println("Authorization went wrong: \(error!.localizedDescription)") } }
Here is my debug log, I get the correct answer:
OAuth2: Handling redirect URL jamesappv2://oauth/callback?state=4B0EB812&code=0sXXXXXXTX7yEbS1XXXXXHuw OAuth2: Successfully validated redirect URL OAuth2: Authorizing against https://login.uber.com/oauth/token?state=38158941&grant_type=authorization_code&code=0sXXXXXXXX1jxTrdFQT9Huw&client_secret=EIXXXXXXXNCa_Wez6XXXXXw0BlnrXtxus&client_id=fXXXXXXXy2LOUo9vtKXXXXXQ1nUDO&redirect_uri=jamesappv2%3A%2F%2Foauth%2Fcallback OAuth2: Exchanging code 0swNXXXXX7yXXXXXXdFQT9Huw with redirect jamesappv2://oauth/callback for token at Optional("https://login.uber.com/oauth/token") OAuth2: Did receive access token: Dfq3XXXXXXuWgpaqFXXXXXXXgXW, refresh token: EmStT7FEXHRMlS8odPzs1nsha0ObjK Did authorize with parameters: [token_type: Bearer, expires_in: 2592000, access_token: XXXXXXOZuWgXXXXXXXXuJYOmgXW, refresh_token: EXXXXXHRMlS8oXXXXXXXa0ObjK, scope: profile, last_authenticated: 1430121470]
Notice that I get a valid access_token
I'm stuck here
According to DOCs says in STEP4 * USE BEARER TOKEN
Pass the access_token returned in the response in the authorization header using the Bearer type to make requests on behalf of the user. *
curl -H 'Authorization: Bearer YOUR_ACCESS_TOKEN' 'https://api.uber.com/v1/products?latitude=37.7759792&longitude=-122.41823'
I do not understand. How do I pass access_token in a header with a media of type? I did as below
func callRequestAPI(url:String){ let request = appDelegate.oauth.request(forURL: NSURL(string:url)!) request.HTTPMethod = "POST" let postString = "product_id="+selectedUberProductId+"&start_latitude="+start_lat+"&start_longitude="+start_lng+"&end_latitude="+end_lat+"&end_longitude="+end_lng println(postString) let tempData: NSData = appDelegate.oauth.accessToken.dataUsingEncoding(NSUTF8StringEncoding)! let base64LoginString = tempData.base64EncodedStringWithOptions(nil) request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.addValue("application/json", forHTTPHeaderField: "Accept") request.setValue("Bearer \(base64LoginString)", forHTTPHeaderField: "Authorization") let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(request) { data, response, error in if error != nil { println("error=\(error)") return } println("response = \(response)") let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) println("responseString = \(responseString)") } task.resume() }
but I get the following answer
response = <NSHTTPURLResponse: 0x1a284b50> { URL: https://sandbox-api.uber.com/v1/requests } { status code: 401, headers { "Content-Length" = 75; "Content-Type" = "application/json"; Date = "Mon, 27 Apr 2015 10:22:01 GMT"; Server = nginx; "Strict-Transport-Security" = "max-age=31536000; includeSubDomains; preload"; "x-uber-app" = "uberex-sandbox"; "x-xss-protection" = "1; mode=block"; } } responseString = Optional({"message":"Invalid OAuth 2.0 credentials provided.","code":"unauthorized"})