Google Api收到错误消息

时间:2013-12-10 13:15:39

标签: ios google-oauth

NSString *clientsec = @"my client sec";
NSString *clientid = @"client id";
NSString *grant = @"urn:ietf:params:oauth:grant-type:migration:oauth1";

NSString *post =[[NSString alloc] initWithFormat:@"client_secret=%@&grant_type=%@&client_id=%@",clientsec,grant,clientid];

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);

我发布的数据是这样的,但我收到这样的错误

 "error" : "invalid_request"

任何人都可以解决我的问题......提前致谢

2 个答案:

答案 0 :(得分:0)

此Google API必须包含5个参数。

code - The authorization code returned from the initial request 
client_id - The client_id obtained during application registration 
client_secret - The client secret obtained during application registration 
redirect_uri - The URI registered with the application 
grant_type - As defined in the OAuth 2.0 specification, this field must contain a value of authorization_code

然后您的实际请求可能如下所示

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code

因此,您必须在请求中添加代码 redirect_uri 参数。

答案 1 :(得分:0)

我试过这个,然后工作:

NSString *urlString = @"https://accounts.google.com/o/oauth2/token";
NSURL *url=[NSURL URLWithString:urlString];

NSString *clientsec = @"my client sec";
NSString *clientid = @"client id";
NSString *grant = @"urn:ietf:params:oauth:grant-type:migration:oauth1";
NSString *post =[[NSString alloc] initWithFormat:@"client_secret=%@&grant_type=%@&client_id=%@",clientsec,grant,clientid];

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

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

urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest* theRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[theRequest setTimeoutInterval:120.0f];

[theRequest setURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPBody:postData];

NSError *error = nil;
NSURLResponse *response = nil;

NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];

NSLog(@"%@",responseData);

<强>输出:

  

client_secret =我的客户   秒&安培; grant_type =瓮:IETF:PARAMS:OAuth的:授予型:迁移:您好!OAuth1&安培; CLIENT_ID =客户端   id 2013-12-10 19:10:07.283 AkbarVenkatConflict [18969:8c03]&lt; 7b0a2020   22657272 6f722220 3a202269 6e76616c 69645f63 6c69656e 74222c0a   20202265 72726f72 5f646573 63726970 74696f6e 22203a20 22426164   20726571 75657374 2e220a7d&gt;