Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have found the answer. Initially I was using the Client ID for an installed application. This did not give the option to set up the Redirect URI. It gave a default Redirect URI of <strong>urn:ietf:wg:oauth:2.0:oob <a href="http://localhost" rel="nofollow noreferrer">http://localhost</a>.</strong> So when I sent a request for authentication using this code:</p> <pre><code>viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope clientID:clientID clientSecret:clientSecret keychainItemName:nil delegate:self finishedSelector:finishedSel]; </code></pre> <p>It returned an success code that is then used to authorize native applications. Please reffer <a href="http://code.google.com/apis/accounts/docs/OAuth2.html" rel="nofollow noreferrer">here</a> for more on returned codes or tokens.</p> <p>To fix my issue I went ahead and used a Client ID for web application. This allowed me to explicitly set a Redirect URI and also allowed me to set the response_type to token instead of code here: </p> <pre><code>https://accounts.google.com/o/oauth2/auth? client_id=21302922996.apps.googleusercontent.com&amp; redirect_uri=https://www.example.com/back&amp; scope=https://www.google.com/m8/feeds/&amp; response_type=**token** </code></pre> <p>So when I am authenticated and the redirect_uri is served back from the server it comes with an "access_tocken" appended as a query string like this:</p> <pre><code>https://www.example.com/back?access_token=returned_access_tocken </code></pre> <p>Now you can use a regular expression code:</p> <pre><code>-(void)checkForAccessToken:(NSString *)urlString { NSError *error; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"access_token=(.*)&amp;" options:0 error:&amp;error]; if (regex != nil) { NSTextCheckingResult *firstMatch = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])]; if (firstMatch) { NSRange accessTokenRange = [firstMatch rangeAtIndex:1]; NSString *accessToken = [urlString substringWithRange:accessTokenRange]; accessToken = [accessToken stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [_delegate accessTokenFoundGoogle:accessToken]; accessTokenFound = YES; } } } </code></pre> <p>to pluck the access_code and use it for your authorization request here:</p> <pre><code>"https://www.googleapis.com/oauth2/v1/userinfo?oauth_token=put_your_accesstoken_here" to send a request for authorization </code></pre> <p>Then you can just send your request and get back in this case the user's profile info in JSON format. You can refer to this <a href="https://stackoverflow.com/questions/3498631/starting-out-with-an-application-for-facebook-connect-on-the-iphone">question and answer</a> for using Facebook's graph api to login users. Then just modify the code to work with the new Google OAuth 2.0 using the suggestions and request urls I have included here. Just a suggestion to speed things for you when converting the code for Facebook, create a new init method like this:</p> <pre><code>- (id)initWithDelegate:(id&lt;GoogleLoginDialogDelegate&gt;)delegate; - (id)initWithDelegate:(id&lt;GoogleLoginDialogDelegate&gt;)delegate { if ((self = [super initWithNibName:@"GoogleLoginDialog" bundle:[NSBundle mainBundle]])) { self.delegate = delegate; } return self; } </code></pre> <p>So that you can easly work with the delegate methods from your Google login dialog. If you follow the Facebook example carefully, you should have Google login with OAuth working great!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload