amz-tools / amazon-sp-api

Amazon Selling Partner API Client
MIT License
230 stars 119 forks source link

When I try to sbuscribe to a notification I get error #98

Open eternalparquet opened 2 years ago

eternalparquet commented 2 years ago

When I try yo subscribe to a notification after I succesfully created a destination id I get this error: {'code': 'InvalidInput', 'message': 'Request has missing or invalid parameters and cannot be parsed.', 'details': 'No destination with destinationid for applicationId appID'} I am using python, this is the code:

subscr = Notifications(marketplace=Marketplaces.IT).create_subscription(
    notification_type=NotificationType.ANY_OFFER_CHANGED,
    destination_id="destinatioid"
    )
amz-tools commented 2 years ago

Hi @eternalparquet,

I don't think we can help you with python, if you are using a python client you should probably ask the guys who created the python client.

eternalparquet commented 2 years ago

then I tried with node to create a destination and I get:

CustomError: Could not find the following credentials in environment variables: SELLING_PARTNER_APP_CLIENT_ID,SELLING_PARTNER_APP_CLIENT_SECRET,AWS_SELLING_PARTNER_ACCESS_KEY_ID or AWS_ACCESS_KEY_ID,AWS_SELLING_PARTNER_SECRET_ACCESS_KEY or AWS_SECRET_ACCESS_KEY,AWS_SELLING_PARTNER_ROLE

 var destinationData = await (async() => {
    try {
      let sellingPartner = new SellingPartnerAPI({
        region:'eu', 
        refresh_token: "token",
        options:{
          auto_request_tokens:false,
          only_grantless_operations:true,
        },
      });
      await sellingPartner.refreshAccessToken('sellingpartnerapi::notifications');
      await sellingPartner.refreshRoleCredentials();
      let res = await sellingPartner.callAPI({
        operation:'createDestination',
        endpoint:'notifications',
        query:{
          name: "amazon-price-adjuster",
          resourceSpecification:
          {
            sqs:
            {
              arn: "arn"
            }
          }        
        }               
      });
      return res;

    } catch(e){
      console.log(e);
      return e;
    }
  })();
  return destinationData; 
amz-tools commented 2 years ago

Okay. To the last comment: You will have to specify your credentials either as environment variables, via a file or directly inside the constructor config options when creating the SellingPartnerAPI class.

eternalparquet commented 2 years ago

ok now I'm getting this error:

CustomError: null
    at SellingPartner.callAPI (/node_modules/amazon-sp-api/lib/SellingPartner.js:578:13)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async /nodejsserver/server.js:27:17
    at async Server.<anonymous> (/nodejsserver/server.js:8:25) {
  code: 'InvalidInput',
  type: 'error'
amz-tools commented 2 years ago

@eternalparquet, its a POST request, so you need to specify the params as body parameters, not query.

eternalparquet commented 2 years ago

Thank you, one more. How to provide the path parameters: notificationType ?

amz-tools commented 2 years ago

Just like the body, but as path:

path:{
  notificationType:'ANY_OFFER_CHANGED'
}
eternalparquet commented 2 years ago

Unfortunately I am getting: details: 'No destination with id DESTINATIONID for applicationId APPID',

amz-tools commented 2 years ago

I think we would need the chain of calls you made in order to be of any help with the error.

eternalparquet commented 2 years ago

Simply I call the api for create destination id. Then I call the create subscription api with the previously obtained destination id.

 var destinationData = await (async() => {
    try {
      let sellingPartner = new SellingPartnerAPI({
        region:'eu', 
        refresh_token: "mytoken",
        options:{
          auto_request_tokens:false,
          only_grantless_operations:true,
        },
        credentials:{
              SELLING_PARTNER_APP_CLIENT_ID: "xxx",
              SELLING_PARTNER_APP_CLIENT_SECRET: "xxx",
              AWS_ACCESS_KEY_ID: "xxx",
              AWS_SECRET_ACCESS_KEY: "xxx",
              AWS_SELLING_PARTNER_ROLE: "xxx"
        }
      });
      await sellingPartner.refreshAccessToken('sellingpartnerapi::notifications');
      await sellingPartner.refreshRoleCredentials();
      let res = await sellingPartner.callAPI({
        operation:'createDestination',
        endpoint:'notifications',
        body: {
          name: "myname",
            resourceSpecification:
          {
            sqs:
            {
              arn: "xxx"
            }
          }  
        }
      return res;

    } catch(e){
      console.log(e);
      return e;
    }
  })();

await (async() => {
    try {
      let sellingPartner = new SellingPartnerAPI({
        region:'eu', 
        refresh_token: "mytoken",
        credentials:{
              SELLING_PARTNER_APP_CLIENT_ID: "xxx",
              SELLING_PARTNER_APP_CLIENT_SECRET: "xxx",
              AWS_ACCESS_KEY_ID: "xx",
              AWS_SECRET_ACCESS_KEY: "xx",
              AWS_SELLING_PARTNER_ROLE: "xxx"
        }
      });
      await sellingPartner.refreshAccessToken('sellingpartnerapi::notifications');
      await sellingPartner.refreshRoleCredentials();
      let res = await sellingPartner.callAPI({
        operation:'createSubscription',
        endpoint:'notifications',
        path:{
             notificationType:'ANY_OFFER_CHANGED'
        },
        body:{
          payloadVersion:"1.0",
          destinationId: "82832c69-f797-4257-a2ad-81bbb07d71f2"
        }             
      });
      return res;

    } catch(e){
      console.log(e);
      return e;
    }
  })();

I don't know if it has anything to do with it but the app is self authorized.

Is there something wrong with the amazon sqs queue policy, please can you check it:

{
  "Version": "2012-10-17",
  "Id": "__default_policy_ID",
  "Statement": [
    {
      "Sid": "__owner_statement",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::437568002678:root"
      },
      "Action": [
        "sqs:GetQueueAttributes",
        "sqs:SendMessage"
      ],
      "Resource": "arn:aws:sqs:eu-west-1:ACCOUNTID:amazon-seller-notifications"
    }
  ]
}
amz-tools commented 2 years ago

@eternalparquet We haven't used notifications endpoint much yet. The calls themselves look ok to me, although you could skip defining refresh_token when creating the instance since these are grantless operation calls. And you also wouldn't have to create two instances of the class, you could just reuse the one you created in the first step for the createSubscription call as well. Concerning the policy definition I don't think we can be of any help, sorry.

eternalparquet commented 2 years ago

Thank you I have obtained the subscription id, now how to listen for events? The documentation doesn't explain that.