1

这是使用 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. 用户输入电子邮件/密码登录 Nest 4. 提示“Works with Nest” 5. 用户“接受” 6. 重定向到 localhost 7. 被拦截以获取“授权码" 8. 使用 NSUrlConnection 传递 Auth code 获取 Auth Token 9. 获取 oauth2_error: "authorization code not found"

4

1 回答 1

0

查看 Nest 开发者网站上的示例iOS-NestDK项目。具体来说,请查看 NestAuthManager 文件,并随时在您的项目中使用它。

干杯,雷蒙德

于 2014-07-23T17:37:24.890 回答