我正在制作一个iOS应用程序,该应用程序使用用户的Google帐户从他的YouTube帐户中获取数据并显示它们.... 第一步是使用gtm2来验证用户,并获得一个访问令牌和一个刷新令牌 问题是访问令牌在60分钟后过期,我必须登录并再次允许应用程序... 我发现你可以使用刷新令牌获得一个新的访问令牌 使用这个从documetation: - >我的问题是如何使一个POST请求获取访问令牌在objective-c 这是我需要使用的数据:如何刷新iOS中的谷歌oauth 2.0获得的令牌
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
这是我使用的代码:
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);
我得到的错误是: { “错误”: “INVALID_REQUEST” }
我不熟悉Objective-C的客户端库,但大多数其它语言的客户端库中有自动刷新的OAuth 2令牌支持为您提供使用过期访问令牌的请求。你确定你需要手动刷新吗?如果是这样,要问的最好的地方可能是https://groups.google.com/forum/?fromgroups#!forum/gdata-objectivec-client –