API访问
API访问
要使用开发者控制台API,您需要创建一个安全配置文件并将该安全配置文件映射到API。安全配置文件是用于为API访问生成访问令牌的机制。
注意: 您的亚马逊开发者控制台账户必须具有管理员级别凭证才能创建安全配置文件和请求API访问。
创建安全配置文件
要创建安全配置文件,请执行以下步骤:
- 登录亚马逊开发者控制台账户。如果您还没有账户,系统将提示您创建账户。
- 在主导航中,单击应用与服务。
- 单击子菜单中的API访问。
-
单击API的名称。
- 单击创建新的安全配置文件按钮。
- 针对新配置文件输入“安全配置文件名称”和“安全配置文件描述”,然后单击保存。
- 保存您的客户端ID和客户端密钥(在Web设置选项卡中),因为您需要此信息才能访问销售报告API。
将安全配置文件映射到API
要将安全配置文件映射到API,请执行以下步骤:
- 返回到API访问页面。
- 单击API名称以选择API。
- 从下拉列表中选择新的安全配置文件。
- 选择附加以将安全配置文件与此API关联。API名称和附加的安全配置文件将添加到Security Profile(s) in use(使用中的安全配置文件)面板。
现在可以使用客户端ID和客户端密钥请求Login With Amazon (LWA)访问令牌。
请求LWA访问令牌
借助客户端ID和客户端密钥,按照以下步骤使用Login With Amazon API请求Login with Amazon访问令牌:
1.发送令牌请求
将POST请求发送到https://api.amazon.com/auth/o2/token
,其中包含以下标头和内容:
- 标头:
Content-Type: application/x-www-form-urlencoded
- 内容:
client_id
: 在创建安全配置文件的步骤7中保存的客户端ID。client_secret
: 在创建安全配置文件的步骤7中保存的客户端密钥。grant_type
: 设置为client_credentials
。scope
: 将值设置为appstore::apps:readwrite
(对于报告API,则设置为adx_reporting::appstore:marketer
)。
示例JSON内容:
{
"grant_type": "client_credentials",
"client_id": "amzn1.application-oa2-client.<your-client-id>",
"client_secret": "<your-client-secret>",
"scope": "appstore::apps:readwrite"
}
示例cURL请求:
curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' -d 'grant_type=client_credentials&client_id=amzn1.application-oa2-client.<your-client-id>&client_secret=<your-client-secret>=appstore::apps:readwrite' https://api.amazon.com/auth/O2/token
2.保存响应
响应如下所示:
{
"access_token": "Atc|MAEBI...",
"scope": "appstore::apps:readwrite",
"token_type": "bearer",
"expires_in": 3600
}
access_token
: 访问令牌。expires_in
: 访问令牌到期之前的秒数。scope
: 将为appstore::apps:readwrite
(对于报告API,则为adx_reporting::appstore:marketer
)。token_type
: 将始终为bearer
。
3.处理任何错误响应
如果令牌请求导致了错误,则响应消息正文包含以下错误消息之一:
错误消息正文 | 详情 |
---|---|
{"error_description":"Client authentication failed", "error":"invalid_client"} | 密钥无效 |
{"error_description":"The request has an invalid parameter : scope", "error":"invalid_scope"} | 范围值无效 |
{"error_description":"The authorization grant type is not supported by the authorization server", "error":"unsupported_grant_type"} | 授权类型不正确 |
{"error_description":"The Content-Type is not supported by the authorization server", "error":"invalid_request"} | content-type不受支持 |