27

I have been trying for ages now to get OAuth 2.0 integration in my iPhone application.

I have searched and searched for libraries, tutorials, etc... But they have all lead me to a dead end. The main problem I have is that they either have deprecated code, or they just don't work or they have some documentation but its really hard to follow (for me anyway...).

The best OAuth2 library I could find for Xcode is this one: https://github.com/nxtbgthng/OAuth2Client

But the main problem with that one is it doesn't seem to do anything... I have followed all the documentation and instructions that came with it, but after building and running, it doesn't seem to authenticate....

So I guess my main question is: does anyone know of any good and up to date OAuth 2.0 tutorials for Xcode or any libraries for such a thing?

Because I am really struggling at the moment.... :(

Thanks for your time, Dan.

UPDATE 2: Here is my code (App Id and secret removed for security):

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

oauthClient = [[LROAuth2Client alloc]
               initWithClientID:@"MY_CLIENT_ID"
               secret:@"MY_APP_SECRET"
               redirectURL:[NSURL URLWithString:@"app://instagram-callback/?code="]];
oauthClient.delegate = self;

oauthClient.userURL  = [NSURL URLWithString:@"https://api.instagram.com/oauth/authorize/?client_id=ab6dc96859bf43b3a488199ec72d9964&redirect_uri=app://instagram-callback/?code=&response_type=code"];
oauthClient.tokenURL = [NSURL URLWithString:@"https://api.instagram.com/oauth/access_token/"];


[oauthClient authorizeUsingWebView:myWebView];


}

- (void)oauthClientDidReceiveAccessToken:(LROAuth2Client *)client;
{
    LROAuth2AccessToken *token = client.accessToken;
    [NSKeyedArchiver archiveRootObject:token toFile:@"Path/To/MyAccessToken"];
}

- (void)checkAccessTokenForExpiry:(LROAuth2AccessToken *)accessToken;
{
    if ([accessToken hasExpired]) {
        [oauthClient refreshAccessToken:accessToken];
    }
}

- (void)oauthClientDidRefreshAccessToken:(LROAuth2Client *)client;
{
    LROAuth2AccessToken *token = client.accessToken;
    [NSKeyedArchiver archiveRootObject:token toFile:@"Path/To/MyAccessToken"];
}
JP Illanes
  • 3,545
  • 41
  • 56
Supertecnoboff
  • 5,886
  • 9
  • 50
  • 94
  • Where is it failing? I assume this is your issue you posted. https://github.com/nxtbgthng/OAuth2Client/issues/82 Does it present a modal view/safari to authenticate with Instagram? or is it a case of literally clicking login and nothing happens? – sbarow Aug 12 '13 at 22:08
  • Answered in your github issue. – sbarow Aug 12 '13 at 22:17
  • @sbarow Thank you for your help. I have a left a comment on the Github page where I posted the issue. Please have a look at it, as the app crashes because it can't get a token. – Supertecnoboff Aug 13 '13 at 16:03
  • Would you mind pasting the code you are using? Maybe use gist? – sbarow Aug 13 '13 at 18:07
  • @sbarow Sure. I have updated the post with the code. Thank you for your help. – Supertecnoboff Aug 13 '13 at 21:32
  • @sbarow The problem now is that my UIWebView doesn't seem to change.... I don't know really. For example, I think the app does now get the token but it doesn't react to the Redirect URL. – Supertecnoboff Aug 13 '13 at 21:36
  • When you say the web view doesn't change? So does it now show a view allowing the user to log in and then once they have logged in it doesn't go away (redirect) ? I think you need to handle dismissing the webview. Have a look at the UIWebView delegate methods, maybe one of them will help you know when to dismiss the view http://developer.apple.com/library/ios/documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html – sbarow Aug 14 '13 at 06:40
  • @sbarow I'm not sure if those methods can help me. Basically, my problem is that my app doesn't seem to register the RedirectURL. So it does present the UIWebView and the user can login, but when they then press authorise in the UIWebView, nothing happens.... (When pressing Authorise the service will provide you or redirect you to your specified RedirectURL... but my app doesn't seem to react to it.... – Supertecnoboff Aug 14 '13 at 09:07
  • Taking a stab at this, Facebook SDK requires you to edit your .plist https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/ 5. Configure a new Xcode Project. look for Configure the .plist I have no idea if that could help. – sbarow Aug 14 '13 at 09:22
  • @sbarow Probably not in this case as Im trying to connect to the Instagram API. – Supertecnoboff Aug 14 '13 at 09:37
  • I would look into this, authorizationURLWithRedirectURL:[NSURL URLWithString:@"myapp://instagram-callback"]; – sbarow Aug 14 '13 at 09:39

5 Answers5

5

In almost all projects I have used AFNetworking because it's very powerful -why re-invent the wheel every time :)

Furthermore, it also has an OAuth2Manager which is quite easy to implement and works rock-solid.

dOM
  • 536
  • 5
  • 14
0

In Instagram documentation says that there are two ways to authenticate. One Explicit (for server-side auth) and one Implicit, for auth in a client (without server).

You are using the Explicit one inside the app, try changing the userURL to https://instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token and the tokenURL to http://your-redirect-uri#access_token=ACCESS-TOKEN.

aramusss
  • 2,271
  • 18
  • 30
0

You most likely cannot use a client secret in an iPhone App (because the client secret cannot be protected), you will need to authenticate through the services mobile app or mobile web site and then handle the redirect url, according to this link I found on the Uber api developer site: http://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified

There is some code on this page to do just that, which I am testing now: https://medium.com/swift-programming/learn-nsurlsession-using-swift-ebd80205f87c

There is also some good information on the bottom part of this page: http://www.idmworks.com/blog/entry/getting-started-with-oauth2client-on-ios

Roger Perkins
  • 188
  • 1
  • 8
0

I recently made simple pod https://github.com/kohtenko/KOSocialOAuth.

You can easily connect Instagram, VK or LinkedIn. Feel free to send Pull Request with any other OAuth endpoint.

Oleg Kohtenko
  • 373
  • 5
  • 14
0

Check out this for using the Instagram API: https://github.com/shyambhat/InstagramKit. In the comments I see that you're having trouble with the redirect - look into Xcode's Redirect URI's for help with that: What's a redirect URI? how does it apply to iOS app for OAuth2.0?

Community
  • 1
  • 1
DHShah01
  • 537
  • 1
  • 7
  • 16