• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Amazon Selling Partner API 开发笔记

其他 沉默♪☞小傲 3074次浏览 0个评论

资料整理

1.sp-api介绍:https://developer.amazonservices.com/

2.github文档:https://github.com/amzn/selling-partner-api-docs

3.github代码:https://github.com/amzn/selling-partner-api-models

4.本文代码:https://github.com/Cxiaoao/amazon-sellingpartner-api

开发准备

1. 登录亚马逊卖家账号申请SP-API开发评估表 https://sellercentral.amazon.com/developer/register
Amazon Selling Partner API 开发笔记 注意:角色上后3项不要勾上,可以少填很多信息
Amazon Selling Partner API 开发笔记

2. 根据 github文档 注册 AWS,文档写的挺不错,可以多去尝试

 Amazon Selling Partner API 开发笔记

 3. 申请通过后添加新的App客户端,类型选SP API,IAM ARN 可以填写AWS IAM User ARN (arn:aws:iam::0000000000000:user/XXXXX) 或 AWS IAM Role ARN (arn:aws:iam::0000000000000:role/XXXXX)

Amazon Selling Partner API 开发笔记

 开发代码

1. 通过 AWS IAM User 调用

只创建AWS IAM User ARN,不创建角色,直接把策略加到User下,这样做不符合规范,但可以少调一次接口

Amazon Selling Partner API 开发笔记

访问级别和访问资源都可以

LWAAuthorizationCredentials lwaAuthorizationCredentials = new LWAAuthorizationCredentials
{
    // 相关值App客户端增加后就会有
    ClientId = "amzn1.application-XXX-client.XXXXXXXXXXXXXXXXXXXXXXX",
    ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    RefreshToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    Endpoint = new Uri("https://api.amazon.com/auth/o2/token")
};
RestClient client = new RestClient("https://sellingpartnerapi-na.amazon.com");

// 获取订单列表
RestRequest request = new RestRequest("/orders/v0/orders", Method.GET);
request.AddQueryParameter("MarketplaceIds", "A2EUQ1WTGCTBG2");
request.AddQueryParameter("CreatedAfter", "2020-12-01T00:00:00Z");
LWAAuthorizationSigner auth = new LWAAuthorizationSigner(lwaAuthorizationCredentials);
auth.Sign(request);

AWSAuthenticationCredentials aws = new AWSAuthenticationCredentials();

// 相关值为AWS IAM增加User后下载的cvs
aws.AccessKeyId = "XXXXXXXXXXXXXXXXXXX";
aws.SecretKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
aws.Region = "us-east-1";
AWSSigV4Signer signer = new AWSSigV4Signer(aws);
signer.Sign(request, client.BaseUrl.Host);

var resp = client.Execute(request);

2. 通过 AWS IAM Role 调用

通过 Github 上创建 AWS IAM User和 AWS IAM Role,这样做符合规范,但要多调一次接口(STS)

Amazon Selling Partner API 开发笔记

 Amazon Selling Partner API 开发笔记

 角色人员是通过角色的信任关系关联,创建角色是配置了Root。

// 相关值为AWS IAM增加User后下载的cvs
var accessKey = "XXXXXXXXXXXXXXXXXXX";
var secretKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var credentials = new BasicAWSCredentials(accessKey, secretKey);
var client = new AmazonSecurityTokenServiceClient(credentials);
var assumeRoleRequest = new AssumeRoleRequest()
{
    // AWS IAM Role ARN
    DurationSeconds = 3600,
    RoleArn = "arn:aws:iam::0000000000000:role/XXXXXXXX",
    RoleSessionName = DateTime.Now.Ticks.ToString()
};
AssumeRoleResponse assumeRoleResponse = await client.AssumeRoleAsync(assumeRoleRequest);

RestClient restClient = new RestClient("https://sellingpartnerapi-na.amazon.com");
IRestRequest restRequest = new RestRequest("/orders/v0/orders", Method.GET);
restRequest.AddQueryParameter("CreatedAfter", "2020-12-01T00:00:00Z");
restRequest.AddQueryParameter("marketplaceIds", "ATVPDKIKX0DER");
var lwaAuthCreds = new LWAAuthorizationCredentials
{
    // 相关值App客户端增加后就会有
    ClientId = "amzn1.application-XXX-client.XXXXXXXXXXXXXXXXXXXXXXX",
    ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    RefreshToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    Endpoint = new Uri("https://api.amazon.com/auth/o2/token")
};
restRequest = new LWAAuthorizationSigner(lwaAuthCreds).Sign(restRequest);
var awsAuthCreds = new AWSAuthenticationCredentials
{
    AccessKeyId = assumeRoleResponse.Credentials.AccessKeyId,
    SecretKey = assumeRoleResponse.Credentials.SecretAccessKey,
    Region = "us-east-1"
};
restRequest.AddHeader("X-Amz-Security-Token", assumeRoleResponse.Credentials.SessionToken);
restRequest = new AWSSigV4Signer(awsAuthCreds)
    .Sign(restRequest, restClient.BaseUrl.Host);
var resp = restClient.Execute(restRequest);

本文代码:https://github.com/Cxiaoao/amazon-sellingpartner-api


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Amazon Selling Partner API 开发笔记
喜欢 (0)

您必须 登录 才能发表评论!

加载中……