Coder Social home page Coder Social logo

oAuth Example in Docs about amazon-sp-api HOT 13 CLOSED

amz-tools avatar amz-tools commented on August 23, 2024
oAuth Example in Docs

from amazon-sp-api.

Comments (13)

Tanveer-LowCoder avatar Tanveer-LowCoder commented on August 23, 2024 3

So, I cracked this, in case someone wants to refer this.

I used the Website workflow, so bear that in mind.

Step 1: Create a button on your web app that opens an external website as follows:

https://sellercentral.amazon.com/apps/authorize/consent?application_id=&state=abc&version=beta

When you create an app on SP, do not forget to add the Auth URL (not important for website flow) and Redirect URL (very important). Configure this redirect URL to capture and store the fields that come back, most important of which is the spapi_auth_code

Step 2: Once you have stored the spapi_auth_code, fire the following POST request:

https://api.amazon.com/auth/o2/token

Header:
Content-Type=application/x-www-form-urlencoded

Params:
grant_type=authorization_code
code=spapi_auth_code from step 1
client_id=Client ID from the developer app
client_secret=Client Secret from the developer app
redirect_uri= same redirect URI as you have added when creating the app within the SP

You get the Refresh Token back - which is what you will use going forward.

You can use this Refresh Token in the package. :)

from amazon-sp-api.

amz-tools avatar amz-tools commented on August 23, 2024

Hi,

as we haven't implemented the oAuth logic into our own project yet I'm afraid we can't provide that for now. As soon as we have though, we will be happy to include it in the docs. Maybe in the meantime somebody else is around who can share an example.

from amazon-sp-api.

kingkong404 avatar kingkong404 commented on August 23, 2024

Hey @amz-tools is the library designed to support clients? I've managed to generate the login URL and get back the success codes but I'm struggling with Step 4 - generating a client using the LWA credentials.

Can the library generate LWA refresh tokens? and generate a client using the LWA credentials or am I going to have to use a 3rd party oAuth library to manage this.

from amazon-sp-api.

amz-tools avatar amz-tools commented on August 23, 2024

Hi @StevenJE,

just realising that my last comment probably wasn't very helpful to the original question. :-)

No, its currently not possible with the library. You can only request a LWA authorization code for customers who have authorized you via MWS before. No Oauth logic included yet.

from amazon-sp-api.

kingkong404 avatar kingkong404 commented on August 23, 2024

No worries! Thanks for the help! :)

from amazon-sp-api.

kingkong404 avatar kingkong404 commented on August 23, 2024

@Tanveer-LowCoder Legend! Thanks a million.

from amazon-sp-api.

Tanveer-LowCoder avatar Tanveer-LowCoder commented on August 23, 2024

I created a AWS server with the code, available at https://rapidapi.com/user/integrationhub in case anyone wants to quickly test the API. I can add more if required.

from amazon-sp-api.

kingkong404 avatar kingkong404 commented on August 23, 2024

@Tanveer-LowCoder Just got round to trying to set this up today and I'm getting 500 Internal Sever Errors returned from https://api.amazon.com/auth/o2/token

x-amzn-ErrorType: InternalFailure:http://internal.amazon.com/coral/com.amazon.coral.service/

Do you know if everything is operational on AWS' end?

Heres my code incase it's something I'm doing.

    const response = await axios.post("https://api.amazon.com/auth/o2/token", null, {
      params: {
        grant_type: "authorization_code",
        code: "ANmZeXXXXXXXM",
        client_id: "amzn1.application-oa2-client.xxxxxxxxxx72",
        client_secret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        redirect_uri: "https://demo.com/success",
      },
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
    });

from amazon-sp-api.

kingkong404 avatar kingkong404 commented on August 23, 2024

Update: It seems to be something to do with Axios setting Content-Length=0 header by default

What library did you use to make the request?

from amazon-sp-api.

kingkong404 avatar kingkong404 commented on August 23, 2024

@Tanveer-LowCoder Ugh I'm still stuck on this... any help would be great.

from amazon-sp-api.

krachtstefan avatar krachtstefan commented on August 23, 2024

@StevenJE I think you called the post method wrong. The second parameter should not be null. This should work:

const response = await axios.post(
  "https://api.amazon.com/auth/o2/token",
  {
    grant_type: "authorization_code",
    code: "ANmZeXXXXXXXM",
    client_id: "amzn1.application-oa2-client.xxxxxxxxxx72",
    client_secret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    redirect_uri: "https://demo.com/success",
  },
  { headers: { "Content-Type": "application/x-www-form-urlencoded" } }
);

from amazon-sp-api.

kingkong404 avatar kingkong404 commented on August 23, 2024

@StevenJE I think you called the post method wrong. The second parameter should not be null. This should work:

const response = await axios.post(
  "https://api.amazon.com/auth/o2/token",
  {
    grant_type: "authorization_code",
    code: "ANmZeXXXXXXXM",
    client_id: "amzn1.application-oa2-client.xxxxxxxxxx72",
    client_secret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    redirect_uri: "https://demo.com/success",
  },
  { headers: { "Content-Type": "application/x-www-form-urlencoded" } }
);

When I do that I get back the following error as it's being sent as data rather than URL params. The null option is to state that no data is being sent.

    data: {
      error_description: 'The authorization grant type is not supported by the authorization server',
      error: 'unsupported_grant_type'
    }

Are you using Axios as well?

from amazon-sp-api.

krachtstefan avatar krachtstefan commented on August 23, 2024

I use the request module, but tested the axios method before posting it. Unfortunately I did not post the right headers:

{ 'Content-Type':'application/json' }

from amazon-sp-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.