NEST API AUTH ERROR - 未找到授权码/ ios

时间:2014-07-11 23:56:58

标签: nest-api

以下是使用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:“找不到授权码”

1 个答案:

答案 0 :(得分:0)

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

干杯, 雷蒙德