UberKit icon indicating copy to clipboard operation
UberKit copied to clipboard

How to get access token after authorisation redirect and callback

Open pxg opened this issue 9 years ago • 7 comments

I'm authorising users of my app using Oauth 2.0 following the documentation https://developer.uber.com/v1/auth/.

After the user authorises the app, it's hitting my web server which then redirects to app using the internal url uberkit://.

I detect this redirect in my app. I have the following function in UberKitDemoAppDelegate.m.

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    NSLog(@"Callback URL received");
    return YES;
}

What function do I need to call once the user is back in the app to get the access token?

Do I need the function in UberKitDemoAppDelegate.m or should the code to get the access token be triggered automatically?

Thanks, Pete

pxg avatar Apr 28 '15 12:04 pxg

You need to make next post request to uber and to get access_token:

curl -F 'client_secret=YOUR_CLIENT_SECRET'
-F 'client_id=YOUR_CLIENT_ID'
-F 'grant_type=authorization_code'
-F 'redirect_uri=YOUR_REDIRECT_URI'
-F 'code=AUTHORIZATION_CODE'
https://login.uber.com/oauth/token

dserkin avatar Apr 29 '15 00:04 dserkin

And actually there is a special function in UberKit.m

- (void) getAuthTokenForCode: (NSString *) code

dserkin avatar Apr 29 '15 01:04 dserkin

@dserkin thanks for your reply. I saw the getAuthTokenForCode function and shouldStartLoadWithRequest in UberKit.m. What I was struggling to understand is how I can call these methods from UberKitDemoAppDelegate.m as there doesn't appear to be a visible interface for either of these methods.

This is why I was asking if I'm handling the internal url callback handling incorrectly.

pxg avatar Apr 29 '15 12:04 pxg

@pxg I gotcha. You are right, method is not public. It supposed to be called during redirection process. But it is never called for me as well. There are some issues. So instead of that I handle redirect on my backend, send auth code back to application and there I make NSURLRequest POST with auth code to obtain access_token.

Ideally we don't need any backend handler. We should be able to catch web view redirect and read ouath code from redirect url on ios app side. But as I mentioned there are some issues in current implementation of UberKit.

dserkin avatar Apr 29 '15 18:04 dserkin

@pxg You may try to implement auth on your side using https://github.com/nxtbgthng/OAuth2Client. Should be easy and good experience.

dserkin avatar Apr 29 '15 18:04 dserkin

Actually I found a useful API in UberKit.h which may help you:

-(BOOL) handleLoginRedirectFromUrl:(NSURL *)url sourceApplication:(NSString *)sourceApplication But there seems to be some bug in this API but I'm still looking into it.

You could test it by calling it in this API in your AppDelegate.m file:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

borischou avatar Jul 03 '15 14:07 borischou

Fix in the pull request. Please review and merge.

smahajan28 avatar Jul 08 '15 11:07 smahajan28