UberKit
UberKit copied to clipboard
How to get access token after authorisation redirect and callback
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
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
And actually there is a special function in UberKit.m
- (void) getAuthTokenForCode: (NSString *) code
@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 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.
@pxg You may try to implement auth on your side using https://github.com/nxtbgthng/OAuth2Client. Should be easy and good experience.
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
Fix in the pull request. Please review and merge.