question

rcast avatar image
rcast asked

Alexa In Skill Products - Access API using GET with Headers (C#)

I have developed a skill and deployed using ASK CLI. The skill works, the in skill purchasing GET request is where I have trouble. I am getting the Exception on my GET request: "There was an error with the connection to amazonException: The remote server returned an error: (403) Forbidden."

This is the GET request in C#. I have verified the apiAccessToken is being rendered where I use it, however, I'm not sure if I'm adding the headers correct for a GET request (maybe POST?):

        public static string GETRequestHeaders(MySkillRequest request)
        {
            string responseText = "";
	string requestUri = @"https://api.amazonalexa.com/v1/users/~current/skills/~current/inSkillProducts";
            advancedClient = WebRequest.Create(requestUri) as HttpWebRequest;
            advancedClient.Method = "GET";
            advancedClient.ContentType = "application/json";
            advancedClient.Headers["Accept-Language"] = "en-US"; // Need to receive this from request, add locale property to MySkillRequest class.
            advancedClient.Headers["authorization"] = request.Context.System.apiAccessToken;


            responseText = advancedClient.RequestUri.ToString();
            responseText += advancedClient.RequestUri.Query;
            responseText += advancedClient.Headers.ToString();


            HttpWebResponse response;
            try
            {
                using (response = advancedClient.GetResponse() as HttpWebResponse)
                {
                    StreamReader str = new StreamReader(response.GetResponseStream());
                    responseText += str.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                responseText += "There was an error with the connection to amazon";
                responseText += "Exception: " + ex.Message;
            }
            return responseText;
        }

ask cliispc#
10 |5000

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

1 Answer

rcast avatar image
rcast answered

This actually works just needed to use fresh apiAccessToken.

3 comments
10 |5000

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

Hi @rcast, glad to see you were able to solve this issue.

Feel free to come back to our forum if you have other questions.

Regards,
Barry

0 Likes 0 ·

Hey... i ran into the same error.. i started with Alexa development just 2-3 weeks back.. i didnt understand what you meant by using fresh apiAccessToken

0 Likes 0 ·
Amazon_Bernardo Bezerra avatar image Amazon_Bernardo Bezerra ♦♦ newuser-0cbde784-ab4a-4dc6-a68e-5a827f7f60ef ·

Hello and thank you for your message.

When sending an HTTP request to Alexa, the value of the Authorization header must be Bearer <apiAccessToken>. The value of the apiAccessToken can be retrieved in the request that your endpoint receives. It is located on event.context.System.apiAccessToken.

You can find more information about the In-Skill Product Service API over here.

Regards,
Barry

0 Likes 0 ·