bizon / selling-partner-api-sdk

A modularized SDK library for Amazon Selling Partner API (fully typed in TypeScript)
https://bizon.github.io/selling-partner-api-sdk/
MIT License
37 stars 7 forks source link

Error using "na" region. #710

Closed jircdev closed 1 year ago

jircdev commented 1 year ago

Hi, first of all, I'd like to thank you for your help.

I'm attempting to configure the selling-partner-api-sdk to retrieve the inventory information from my Amazon seller partner account, but I am encountering an error "region is missing" from the "@aws-sdk" package. I have already defined the region in the instance of "FbaInventoryApiClient." I'm not sure if this is an error or just a bad implementation,

This is my code (All values in upper are constants):

        const auth = new SellingPartnerApiAuth({
            clientId: CLIENT_ID,
            clientSecret: ACCESS_SECRET_KEY,
            refreshToken: REFRESH_TOKEN,
            accessKeyId: ACCESS_KEY_ID,
            secretAccessKey: ACCESS_SECRET_KEY,
            role: {
                arn: ARN,
            },
        });

        const client = new FbaInventoryApiClient({
            auth,
            region: "na", // as the ts docs says.
        });

        const specs = {
            granularityType: "Marketplace",
            granularityId: MARKETPLACE_ID,
            marketplaceIds: [MARKETPLACE_ID],
        };

        client.getInventorySummaries(specs);

I got the next error: image

Trying to debug, I found that the "sts property of the SellingPartnerApiAuth has an undefined value in region property as you can see in the following image: image

Thanks for your answer

tusbar commented 1 year ago

Hey,

The error comes from the AWS sdk. You need to specify an AWS region to the SellingPartnerApiAuth for the STS calls:

 const auth = new SellingPartnerApiAuth({
    clientId: CLIENT_ID,
    clientSecret: ACCESS_SECRET_KEY,
    refreshToken: REFRESH_TOKEN,
    accessKeyId: ACCESS_KEY_ID,
    secretAccessKey: ACCESS_SECRET_KEY,
+   region: 'us‑west‑1', // Or your preferred AWS region
    role: {
        arn: ARN,
    },
});

This can also be done using the AWS_DEFAULT_REGION environment variable.

I’ve updated to clients’ READMEs to include samples for this.

jircdev commented 1 year ago

Thank you. I followed the example in the docs and thought everything was okay since I saw it in the FbaInventoryApiClient.

I have another doubt, and I'm not sure if you can answer it in this thread. I will ask, and please let me know if I need to close the ticket. Thank you for your patience.

I am receiving a 401 error with the message "SellingPartnerApiAuthError: access-token error: Response code 401."

I'm not sure what the correct way to proceed is. I understood that I need to set the "accessToken". While checking out the parameters of SellingPartnerAuth, I saw the "sessionToken." Is it the same? If not, what is the correct way to generate the access token using the package? Can I get the access token using some method?

I have the next code now:

image

In the console of "auth.accessToken", the scopes, value and expiration dates are ":undefined" as I show: image

In the role: arn value of the instance I am setting the SellingPartner role arn. Is it Correct? Thank you very much for your help.

tusbar commented 1 year ago

@jircdev something must be wrong with your configuration values. I’m thinking maybe ACCESS_SECRET_KEY that is both used for clientSecret and secretAccessKey.

clientId and clientSecret are LWA credentials.
accessKeyId and secretAccessKey are AWS credentials.

https://github.com/bizon/selling-partner-api-sdk/tree/master/packages/auth should have all the info you need.

jircdev commented 1 year ago

Thank you for your response. I appreciate your feedback. Another developer has informed me that it may not be possible to directly access inventory data and that working with reports may be necessary. I had attempted to use the Amazon inventory API to retrieve the available quantity of specific SKUs. However, if it is indeed not possible to access the data directly, I will need to implement a cron job instead of using this package. Could you please confirm if this is the correct approach? Thank you once again for your help.