纯净、安全、绿色的下载网站

首页|软件分类|下载排行|最新软件|IT学院

当前位置:首页IT学院IT技术

Amazon Selling Partner API 开发笔记

沉默♪☞小傲   2021-01-25 我要评论

资料整理

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

开发准备

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

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

 

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

 开发代码

1. 通过 AWS IAM User 调用

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

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

代码的相关类库都在https://github.com/amzn/selling-partner-api-models

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)

 

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

通过 nuget 安装 AWSSDK.SecurityToken

代码的相关类库都在https://github.com/amzn/selling-partner-api-models

// 相关值为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);

 


相关文章

猜您喜欢

网友评论

Copyright 2020 www.SiJiMeiShi.com 【四季软件园】 版权所有 软件发布

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 点此查看联系方式