Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No notifications were fired ? #26

Closed
sdrpa opened this issue Mar 19, 2012 · 4 comments
Closed

No notifications were fired ? #26

sdrpa opened this issue Mar 19, 2012 · 4 comments

Comments

@sdrpa
Copy link

sdrpa commented Mar 19, 2012

I tried to test OAuth2Client with demo project (iPhone), using google-php oauth2 server, configured OAuth2Client and called:

[[NXOAuth2AccountStore sharedStore] requestAccessToAccountWithType:@"SampleService"];

Everything seems to be working fine ... I sniffed web traffic, app. opened webview, I approved access, it completed all oauth2 steps and in last step received code (access_token), which was valid (I tried to use it manually) but no notifications were fired.

NXOAuth2AccountStoreAccountsDidChangeNotification ?
NXOAuth2AccountDidFailToGetAccessTokenNotification ?

[[NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreAccountsDidChangeNotification
                                                  object:[NXOAuth2AccountStore sharedStore]
                                                   queue:nil
                                              usingBlock:^(NSNotification *aNotification){
                                                    // Never called ? even when valid access token was received from token endpoint.
                                              }];

Am I missing something or should I parse manually received response from token endpoint to extract access token ?

What I did

I configured client with:

+ (void)initialize;
{
    [[NXOAuth2AccountStore sharedStore] setClientID:@"xXxXxXxXxXxX"
                                             secret:@"xXxXxXxXxXxX"
                                   authorizationURL:[NSURL URLWithString:@"https://...your auth URL..."]
                                           tokenURL:[NSURL URLWithString:@"https://...your token URL..."]
                                        redirectURL:[NSURL URLWithString:@"https://...your redirect URL..."]
                                     forAccountType:@"myFancyService"];
}

in app delegate,

Called [[NXOAuth2AccountStore sharedStore] requestAccessToAccountWithType:@"myFancyService"]; - client did all necessary steps to complete auth. process,

On success I should have received NXOAuth2AccountStoreAccountsDidChangeNotification or NXOAuth2AccountDidFailToGetAccessTokenNotification ...

Everything works but no notifications were fired/received. I need them to close webview and to continue using client for further requests.
Thank you.

@anagromataf
Copy link
Collaborator

Have you used the same name for your service in all the cases? Your example uses "SampleService" and "myFancyService".

@sdrpa
Copy link
Author

sdrpa commented Mar 19, 2012

Yes, checked it. Here is the code inside app. delegate .m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreAccountsDidChangeNotification
                                                  object:[NXOAuth2AccountStore sharedStore]
                                                   queue:nil
                                              usingBlock:^(NSNotification *aNotification){
                                                    NSLog(@"Success!");
                                              }];

    [[NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreDidFailToRequestAccessNotification
                                                  object:[NXOAuth2AccountStore sharedStore]
                                                   queue:nil
                                              usingBlock:^(NSNotification *aNotification){
                                                    NSError *error = [aNotification.userInfo objectForKey:NXOAuth2AccountStoreErrorKey];
                                                    NSLog(@"Failure %@!", [error localDescription]);
                                              }];

    [[NXOAuth2AccountStore sharedStore] requestAccessToAccountWithType:@"SampleService"];

    return YES;
}

+ (void)initialize;
{
    [[NXOAuth2AccountStore sharedStore] setClientID:@"123"
                                             secret:@"321"
                                   authorizationURL:[NSURL URLWithString:@"http://localhost/myapp/authorize.php"]
                                           tokenURL:[NSURL URLWithString:@"http://localhost/myapp/token.php"]
                                        redirectURL:[NSURL URLWithString:@"http://localhost/myapp/dummy.php"]
                                     forAccountType:@"SampleService"];
}

When I run app. I observe that app. makes request to http://localhost/myapp/authorize.php with clientid, secret ... receives code and contacts http://localhost/myapp/token.php with grant_type .... and receives back JSON { access_token:".....", scope: "..." }
It receives valid token ... but block

   [NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreAccountsDidChangeNotification
                                                  object:[NXOAuth2AccountStore sharedStore]
                                                   queue:nil
                                              usingBlock:^(NSNotification *aNotification){
                                                    NSLog(@"Success!");
                                              }];

is never executed ?

Can you confirm that the code bellow:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreAccountsDidChangeNotification
                                                  object:[NXOAuth2AccountStore sharedStore]
                                                   queue:nil
                                              usingBlock:^(NSNotification *aNotification){
                                                    NSLog(@"Success!");
                                              }];

    [[NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreDidFailToRequestAccessNotification
                                                  object:[NXOAuth2AccountStore sharedStore]
                                                   queue:nil
                                              usingBlock:^(NSNotification *aNotification){
                                                    NSError *error = [aNotification.userInfo objectForKey:NXOAuth2AccountStoreErrorKey];
                                                    NSLog(@"Failure %@!", [error localDescription]);
                                              }];

    [[NXOAuth2AccountStore sharedStore] requestAccessToAccountWithType:@"SampleService"];

    return YES;
}

+ (void)initialize;
{
    [[NXOAuth2AccountStore sharedStore] setClientID:@"123"
                                             secret:@"321"
                                   authorizationURL:[NSURL URLWithString:@"http://localhost/myapp/authorize.php"]
                                           tokenURL:[NSURL URLWithString:@"http://localhost/myapp/token.php"]
                                        redirectURL:[NSURL URLWithString:@"http://localhost/myapp/dummy.php"]
                                     forAccountType:@"SampleService"];
}

is enough ?.
Sorry for bothering you but it might be useful for other users.
Thanks in advance.

@anagromataf
Copy link
Collaborator

Are you presenting the web page for authenticating in an external browser? If yes, you need to make sure, that the schema of the redirect URL is handled by your app. You have to register that URL while setting the client id/secret etc. (e.g., "mysampleservice://oauth2/").

Either way you have to call [[NXOAuth2AccountStore sharedStore] handleRedirectURL:...].

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;
{
    BOOL handled = [[NXOAuth2AccountStore sharedStore] handleRedirectURL:[NSURL URLWithString:url]];
    if (!handled) {
        NSLog(@"The URL (%@) could not be handled. Maybe you want to do something with it.", url);
    }
    return handled;
}

This part is missing in our documentation (we have to go over it).
I hope that helps.

Tobias.

@sdrpa
Copy link
Author

sdrpa commented Mar 20, 2012

Thank you, it works perfectly.
For the sake of completeness, here is complete code to make it working in 5 mins for a new app.

First we need to register custom URL scheme for our app. Here is tutorial: http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
Required code for authenticating in an external browser:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreAccountsDidChangeNotification
                                                  object:[NXOAuth2AccountStore sharedStore]
                                                   queue:nil
                                              usingBlock:^(NSNotification *aNotification){
                                                    NSLog(@"Success!");
                                              }];

    [[NSNotificationCenter defaultCenter] addObserverForName:NXOAuth2AccountStoreDidFailToRequestAccessNotification
                                                  object:[NXOAuth2AccountStore sharedStore]
                                                   queue:nil
                                              usingBlock:^(NSNotification *aNotification){
                                                    NSError *error = [aNotification.userInfo objectForKey:NXOAuth2AccountStoreErrorKey];
                                                    NSLog(@"Failure %@!", [error localDescription]);
                                              }];

    [[NXOAuth2AccountStore sharedStore] requestAccessToAccountWithType:@"SampleService"];

    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    BOOL handled = [[NXOAuth2AccountStore sharedStore] handleRedirectURL:url];
    if (!handled) {
       NSLog(@"The URL (%@) could not be handled. Maybe you want to do something with it.", [url absoluteString]);
    }
    return handled;
}

+ (void)initialize;
{
    [[NXOAuth2AccountStore sharedStore] setClientID:@"123"
                                             secret:@"321"
                                   authorizationURL:[NSURL URLWithString:@"http://localhost/myapp/authorize.php"]
                                           tokenURL:[NSURL URLWithString:@"http://localhost/myapp/token.php"]
                                        redirectURL:[NSURL URLWithString:@"sampleservice://oauth2"]
                                     forAccountType:@"SampleService"];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants