Reputation: 31
Here is the code using obj-c ..i am getting "authorization code not found" error but unable to resolve it. i tried couple of alternatives
Error:{"error":"oauth2_error","error_description":"authorization code not found"}
Case 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];
Case 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];
My steps: 1. Used LHOAuth2LoginViewController 2. Opens webview 3. user enters email/pwd to login to Nest 4. "Works with Nest" is prompted 5. User "Accepts" 6. Redirected to localhost 7. Intercepted to get "Authorization code" 8. Use NSUrlConnection to pass Auth code to get Auth Token 9. Getting oauth2_error: "authorization code not found"
Upvotes: 1
Views: 566
Reputation: 43
Have a look at the sample iOS-NestDK project on Nest's developer website. Specifically, take a look at the NestAuthManager file and feel free to use it in your project.
Cheers, Raymond
Upvotes: 0