以下是使用obj-c的代码..我正在获取“未找到授权代码”错误但无法解决它。我尝试了几种替代方案
错误:{“error”:“oauth2_error”,“error_description”:“找不到授权码”}
案例1: NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@“https://api.home.nest.com/oauth2/access_token?code=%@&client_id=xxx&client_secret=xxx&grant_type=authorization_code,code]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
案例2: NSString * post = [NSString stringWithFormat:@“client_id = xxx& client_secret = xxx& grant_type = authorization_code& code =%@”,code];
NSLog(@"Post -%@",post);
NSData * postData = [post dataUsingEncoding:NSUTF8StringEncoding]; //tried ascii too
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"https://api.home.nest.com/oauth2/access_token"]];
NSLog(@"url-%@",url);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
我的步骤: 1.使用LHOAuth2LoginViewController 2.打开webview 3.用户输入email / pwd登录Nest 4.提示“使用Nest” 5.用户“接受” 6.重定向到localhost 7.截获获取“授权码” 8.使用NSUrlConnection传递Auth代码以获取Auth Token 9.获取oauth2_error:“找不到授权码”