I am using the Youtube Api in my iPad application. i managed to get authenticated and get the access token using the OAuth 2.0. my problem is that the token expires after one hour and i don't know how to get a new one using the refresh-token without going through the authentication process again. i am using XCode 4.5 and iOS 5.1 & 6
Asked
Active
Viewed 4,508 times
0
-
possible duplicate of [How to Refresh the token that i got from google oauth 2.0 in iOS](http://stackoverflow.com/questions/14014069/how-to-refresh-the-token-that-i-got-from-google-oauth-2-0-in-ios) – Kate Gregory Jan 02 '13 at 16:20
-
I am working on similar thing, I am not able to get access token using OAuth 2.0, can you please help me to get the access_token? – Milan Gupta Jan 12 '16 at 09:26
2 Answers
3
According do the documentation
If your application obtains a refresh token during the authorization process, then you will need to periodically use that token to obtain a new, valid access token. Server-side web applications, installed applications, and devices all obtain refresh tokens.
So if you already have your refresh token, you just need to perform a POST
request configured as follows
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=<YOUR CLIENT SECRET>
refresh_token=<YOUR REFRESH TOKEN>
grant_type=refresh_token
and you'll get back a response like
{
"access_token":<A NEW ACCESS TOKEN>,
"expires_in":<AN EXPIRING TIME>,
"token_type":"Bearer"
}

Gabriele Petronella
- 106,943
- 21
- 217
- 235
-
+Gabriele i tried that too : can you look at this [post](http://stackoverflow.com/questions/14014069/how-to-refresh-the-token-that-i-got-from-google-oauth-2-0-in-ios) – Rifinio Jan 02 '13 at 14:37
-
I'll take a look at that but duplicating your own question is not a good practice – Gabriele Petronella Jan 02 '13 at 14:43
-
it is not duplicated ! the first one was to know the proper way on how to refresh it using a POST Request. and this one is about the whole approach .. so don't get confused ! – Rifinio Jan 02 '13 at 14:49
-
it's surely more than related. Next time edit your original question. – Gabriele Petronella Jan 02 '13 at 15:05
3
Here is the full code to refresh the accessToken using AFNetworking to make the request:
NSString *refreshToken = <YOUR_REFRESH_TOKEN>;
NSString *post = [NSString stringWithFormat:@"client_secret=%@&grant_type=refresh_token&refresh_token=%@&client_id=%@",kYouTubeClientSecret,refreshToken,kYouTubeClientID];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:@"https://accounts.google.com/o/oauth2/token"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
AFHTTPRequestOperation *httpRequest = [httpClient HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
NSString *newAccessToken = json[@"access_token"];
NSLog(@"received new accessToken = %@",newAccessToken);
// store accessToken here
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error refreshing token: %@",[error localizedDescription]);
}];
[httpClient enqueueHTTPRequestOperation:httpRequest];

jj0b
- 1,106
- 1
- 12
- 19
-
1thanks for this, resolved a 2 hour problem! I was initially using a dictionary to create the parameters and passing that as the body. But was giving me error that grant_type was no set. Your method of using the NSString with concatenated parameters is what resolved it. – sudoExclaimationExclaimation Jan 24 '16 at 08:53