nxtbgthng / OAuth2Client

Client library for OAuth2 (currently built against draft 10 of the OAuth2 spec)
855 stars 217 forks source link

It doesn't do anything :( #82

Open Supertecnoboff opened 11 years ago

Supertecnoboff commented 11 years ago

Hi,

I have followed all the instructions and it doesn't work. The app builds perfectly fine, but literally nothing happens...

I am trying to connect to Instagram via OAuth 2.0, I thought this library would help/

Please help. Thanks. Here is my code:

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

/*[[NXOAuth2AccountStore sharedStore] requestAccessToAccountWithType:@"instagram"
                               withPreparedAuthorizationURLHandler:^(NSURL *preparedURL){
                                   // Open a web view or similar
                               }];

 */

[self initialize];

[[NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreAccountsDidChangeNotification
                                                  object:[NXOAuth2AccountStore sharedStore]
                                                   queue:nil
                                              usingBlock:^(NSNotification *aNotification){
                                                  // Update your UI

                                                  NSLog(@"Sucess");
                                                  webviewer.alpha = 0.0;
                                              }];

[[NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreDidFailToRequestAccessNotification
                                                  object:[NXOAuth2AccountStore sharedStore]
                                                   queue:nil
                                              usingBlock:^(NSNotification *aNotification){
                                                  NSError *error = [aNotification.userInfo objectForKey:NXOAuth2AccountStoreErrorKey];
                                                  // Do something with the error

                                                  NSLog(@"Error: %@", error);
                                              }];

}

sbarow commented 11 years ago

From the documentation.

This should be in your AppDelegate.

[[NXOAuth2AccountStore sharedStore] setClientID:@"ab6dc96859bf43b3a488199ec72d9964"
secret:@"78aaaf1d99534bcb9cecbb09b3215580"
scope:[NSSet setWithObjects:@"likes", @"relationships", @"comments", nil]
authorizationURL:[NSURL URLWithString:@"https://api.instagram.com/oauth/authorize"]
tokenURL:[NSURL URLWithString:@"https://api.instagram.com/oauth/access_token"]
redirectURL:[NSURL URLWithString:@"myapp://instagram-callback"]
forAccountType:@"Instagram"];

Make your viewDidLoad method look like this...

[[NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreAccountsDidChangeNotification
                                                  object:[NXOAuth2AccountStore sharedStore]
                                                   queue:nil
                                              usingBlock:^(NSNotification *aNotification){
                                                  // Update your UI

                                                  NSLog(@"Sucess");
                                                  webviewer.alpha = 0.0;
                                              }];

[[NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreDidFailToRequestAccessNotification
                                                  object:[NXOAuth2AccountStore sharedStore]
                                                   queue:nil
                                              usingBlock:^(NSNotification *aNotification){
                                                  NSError *error = [aNotification.userInfo objectForKey:NXOAuth2AccountStoreErrorKey];
                                                  // Do something with the error

                                                  NSLog(@"Error: %@", error);
                                              }];

[[NXOAuth2AccountStore sharedStore] requestAccessToAccountWithType:@"Instagram"
withPreparedAuthorizationURLHandler:^(NSURL *preparedURL){
// Open a web view or similar

                               [webviewer loadRequest:[NSURLRequest requestWithURL: preparedURL]];

                           }];
}
Supertecnoboff commented 11 years ago

@stigi @sbarow Thanks a lot man that helped me a lot. Just one question though, the app does now crash because it complains that it can't get a token. And I think it can't get a token because Instagram only grants a token when the user has logged in via the web view. Is there anyway to get the user to login via the webview first or something along those lines?

stigi commented 11 years ago

@sbarow the +initialize part is (seriously) in the documentation.

Please understand that we can't give you sample code for every API this library might be thrown against. OAuth2 is a huge mess, and everyone is doing it differently. We can't support each providers implementation. There was a plan to collect a list of APIs this framework is known to work with, but we've not gotten that far yet.

When we developed it we we're working against a hand full of APIs (SoundCloud, Eyeem,...) and most of them weren't a problem. Instagram should also work, but I haven't implemented that myself (maybe @toto can give a hint, if there's anything tricky).

But please when you open an issue, don't just dump us a kilobyte of code and expect us to just fix it. This is not StackOverflow.

PS: please also take some time and learn to write Markdown. It's not that hard and makes your issues much more readable (which increases the motivation to deal with them).

Supertecnoboff commented 11 years ago

@stigi Ok right I understand. My point was more, if you could make an example app with just one service of your choice for example SoundCloud, Twitter, Facebook, etc... one of those. This would then help people like me to see how you used OAuth2Client.

Anyway, more to the point, do you maybe know why or how I could get OAuth2Client to authorise via a UIWebView first. Because I'm following your documentation which says to run the initialise method in the Application Delegate. I am now doing that.

And while this is all great, it still crashes due to not being able to get a token. I don't think ANY OAuth 2.0 service just gives a token until the user has logged in because its only once you login via their webpage that you get a redirect URI and the end part of the redirect URI has a code. The documentation of Instagram says you need to get this code and add it to the token URL (I think) to then get the access_token.

Do you by any chance have any advice for me?

Thank you for your time and I am sorry you feel that I am just dumping my code on you. The reason I posted here was that you are the author and so I thought you would be the best person to ask since you would know the most about your own API. Thank you for your time.