Search code examples
javascriptreactjsfetchalexa-skills-kit

Fetch to alexa skill activation api does not work


I am creating an Alexa skill with account linking. i managed to get the access token, but now I try to call the Alexa Skill Activation API, and I always get an error. I am putting my code and my error in here:

  fetch(`https://api.amazonalexa.com/v1/users/~current/skills/{SkillId}/enablement`, {
    method: 'post',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${access_token}`
    },
    body: JSON.stringify({
      stage: "development",
      accountLinkRequest: {
        redirectUri: "https://my.url.com/alexa/",
        authCode: code,
        type: "AUTH_CODE"
      }
    })   
  }).then(res=>res.json()).then(response=>{
    alert(JSON.stringify(response));
  }).catch(err=>alert(err));

enter image description here

What should I do?


Solution

  • You added react as a tag and fetch is more traditionally a browser API (though it can be imported into node), so I'm guessing you may be doing this in the browser.

    That would cause CORS issues as using fetch in the browser will always add an Origin header to a fetch request, triggering a CORS check. That's why a lot of requests that work in cURL will break with fetch.

    The docs specifically say this API call should come from a 'back-end server,' so if it's a browser-based fetch, you probably have your culprit.