Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Refresh the token that i got from google oauth 2.0 in iOS

I am making an iOS application that uses the user's google account to get data from his youtube account and show them .... first step is done using the gtm2 to authenticate the user and get an acces-token and a refresh-token the problem is that the access-token expires after 60 minutes and i have to login and allow the application again... i have found that you can use the refresh-token to get a new access-token using this from the documetation : --> my question is how to make a POST request to get the access token in objective-c this is the data i need to use:

POST /o/oauth2/token HTTP/1.1

Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

client_id=21302922996.apps.googleusercontent.com&
client_secret=XTHhXh1SlUNgvyWGwDk1EjXB&
refresh_token=1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ&
grant_type=refresh_token

this is the code i am using :

NSString *post =[[NSString alloc] initWithFormat:@"client_secret=%@&grant_type=refresh_token&refresh_token=%@&client_id%@",kGoogleClientSecretKey,kRefreshToken,kGoogleClientIDKey];
    NSLog(@"%@",post);
    NSURL *url=[NSURL URLWithString:@"https://accounts.google.com/o/oauth2/token"];

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];


    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"%@",data);

the error i get is : { "error" : "invalid_request" }

like image 534
Rifinio Avatar asked Dec 23 '12 19:12

Rifinio


People also ask

How do I get a new refresh token OAuth2?

Because OAuth2 access expires after a limited time, an OAuth2 refresh token is used to automatically renew OAuth2 access. Click the tab for the programming language you're using, and follow the instructions to generate an OAuth2 refresh token and set up the configuration file for your client.

How do I refresh my access token?

To get a refresh token, you must include the offline_access scope when you initiate an authentication request through the /authorize endpoint. Be sure to initiate Offline Access in your API. For more information, read API Settings.


1 Answers

You are missing a = in your format string.

Change

NSString *post =[[NSString alloc] initWithFormat:@"client_secret=%@&grant_type=refresh_token&refresh_token=%@&client_id%@",kGoogleClientSecretKey,kRefreshToken,kGoogleClientIDKey];

to

NSString *post =[[NSString alloc] initWithFormat:@"client_secret=%@&grant_type=refresh_token&refresh_token=%@&client_id=%@",kGoogleClientSecretKey,kRefreshToken,kGoogleClientIDKey];
like image 54
Gabriele Petronella Avatar answered Sep 19 '22 17:09

Gabriele Petronella