question

Shang Ting, Yang avatar image
Shang Ting, Yang asked ·

Failed to get token

I follow the instructions to create an iOS App for AVS by the following link. https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/docs/authorizing-your-alexa-enabled-product-from-an-android-or-ios-mobile-app I can get authorizationCode from authorizeUserForScopes. However, I stuck in step 7. I update requestDidSucceed for the following code: - (void)requestDidSucceed:(APIResult *)apiResult { NSString* authorizationCode = apiResult.result; NSLog(@"authorizationCode: %@", authorizationCode); NSString * codeVerifier = @"123456789012345678901234567890123456789012345678901234567890"; NSString *post =[[NSString alloc] initWithFormat:@"grant_type=%@&code=%@&redirect_uri=%@&client_id=%@&code_verifier=%@", @"authorization_code", authorizationCode, [AIMobileLib getRedirectUri], [AIMobileLib getClientId], codeVerifier]; NSURL *url=[NSURL URLWithString:@" https://api.amazon.com/auth/O2/token"]; NSLog(@"Post data: %@", post); NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [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(@"Rsp: %@",data); } When I post the data to get token. I always get following response: {"error_description":"Not authorized for requested operation","error":"unauthorized_client"} Any comment?
alexa voice service
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

elstryan@Amazon avatar image
elstryan@Amazon answered ·
Hello Shang, Stepping back: It looks like you are trying to get and use an AVS access token on your iPhone/iOS device - there is a more direct route for this. Modify the code in step 5 to: NSArray *requestScopes = [NSArray arrayWithObjects:@"alexa:all", nil]; NSMutableDictionary *options = [[NSMutableDictionary alloc] init]; NSString* scopeData = [NSString stringWithFormat:@"{\"alexa:all\":{\"productID\":\"%@\"," "\"productInstanceAttributes\":{\"deviceSerialNumber\":\"%@\"}}}", productId, deviceSerialNumber]; options[kAIOptionScopeData] = scopeData; Then call authorize. Now after the user has authenticated you will get an access token instead of an authorization code. You can then proceed to use AVS right away. The link that you attached is written for the use case where you wanted to generate an authorization code on your mobile app, and pass the Login with Amazon OAuth 2.0 authorization code to an Alexa-enabled product (another physical device that is NOT your iPhone) so that the Alexa-enabled product can be authorized to access the Alexa Voice Service APIs on behalf of the authorizing user. From what I've read you're attempting to use AVS directly on your iPhone so this step should work.
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

elstryan@Amazon avatar image
elstryan@Amazon answered ·
Also: If you really want to continue with the source you posted use this instead: Change the following two lines: NSString *post =[[NSString alloc] initWithFormat:@"{\"grant_type\":\"%@\", \"code\":\"%@\", \"redirect_uri\":\"%@\", \"client_id\":\"%@\", \"code_verifier\":\"%@\"}", @"authorization_code", authorizationCode, [AIMobileLib getRedirectUri], [AIMobileLib getClientId], codeVerifier]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; However, as I mentioned this is a round about way to get an access token on your iOS
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Shang Ting, Yang avatar image
Shang Ting, Yang answered ·
After I update the code you mention: :

NSString *post =[[NSString alloc] initWithFormat:@"{\"grant_type\":\"%@\", \"code\":\"%@\", \"redirect_uri\":\"%@\", \"client_id\":\"%@\", \"code_verifier\":\"%@\"}",
@"authorization_code",
authorizationCode,
[AIMobileLib getRedirectUri],
[AIMobileLib getClientId],
codeVerifier];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

I still get the same error:
{"error_description":"Not authorized for requested operation","error":"unauthorized_client"}
Looks like I still use incorrect client id.
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Shang Ting, Yang avatar image
Shang Ting, Yang answered ·
I also try to get an access token directly by the following code after login. AMZNGetAccessTokenDelegate * delegate = [[AMZNGetAccessTokenDelegate alloc] initWithParentController:parentViewController]; NSArray *requestScopes = [NSArray arrayWithObjects:@"alexa:all", nil]; [AIMobileLib getAccessTokenForScopes:requestScopes withOverrideParams:nil delegate:delegate]; An access token is returned and I post another request to test AVS. However, I got another error:

400 Bad Request


Server Here is the data for the request: URL: https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize http header dump in json format: { Authorization = "Bearer Atza|IQEBLjAsAhQK-7TYF2xJMzY3pC8RiwEJLUnq_AIUBA23zIjLvPf2wT2iq_xGwWM8RoyGme0YtCXWZ_LIFm57jzwIyfwT8mrPjZZCxkf28ij6kHF-_rFxT_RjiG5lCH5SAACBKIIA6HtFOgeW9FdFnHKZHiO5Z2DyHBp8TnkUpwsaUFbQzGiLZPjO29ShtNvcjg_jUQ__PRx5HIOuFmoxNjK2Pd1XWGay5lV-rOD7TEbMKxVU0DSQ8yOZSEOlPJ4MwSIee_qrm6N1p8Fi9NpQaEr8LFviEduOabzndN-XK2C31XGvM8yh1q6LIOCiUY-nLwdbutjYHuZweLqMJtQPeqEa5jMunlHa7MrzgzbSpMjT3gyukiO_i6DsVjwHB_zSJ9ukuuI6itGFAQiYlHVeIHeaYw"; "Content-Length" = 691; "Content-Type" = "multipart/form-data; boundary=THISISTHEBOUNDARY1234"; "Transfer-Encoding" = chunked; } http body: --THISISTHEBOUNDARY1234 Content-Disposition: form-data; name="metadata" Content-Type: application/json; charset=UTF-8 { "messageBody" : { "profile" : "doppler-scone", "locale" : "en-us", "format" : "audio\/L16; rate=16000; channels=1" }, "messageHeader" : { "deviceContext" : [ { "name" : "playbackState", "namespace" : "AudioPlayer", "payload" : { "offsetInMilliseconds" : "0", "streamId" : "", "playerActivity" : "IDLE" } } ] } } --THISISTHEBOUNDARY1234 Content-Disposition: form-data; name="audio" Content-Type: audio/L16; rate=16000; channels=1 { {audio data, not list in this thread}} --THISISTHEBOUNDARY1234-- Message was edited by: Shang Ting, Yang
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Shang Ting, Yang avatar image
Shang Ting, Yang answered ·
After doing the following changes - remove Content-Length from http header - set http body by setHTTPBodyStream I can get 200 ok from response now.
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

some-amazonian-user avatar image
some-amazonian-user answered ·
@Shang Ting Would it be possible for you to share your implementation code you used to get the api call to https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize to work? I'm working on integrating AVS on an ios app and having some issues trying to get a 200 response back. Thanks!
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.