目的:获取访问Advertising API 的 access_token,refresh_token
先决条件:见《一、创建安全配置文件(Amazon Advertising API 授权)》
步骤1:获取授权码
- Amazon Advertising API授权代码用于生成 authorization 和 refresh tokens。授权代码在调用API的区域中生成。 用于检索授权码的API调用在每个区域都有不同的URL
区域 | URL 前缀 |
---|---|
North America (NA) | https://www.amazon.com/ap/oa |
Europe (EU) | https://eu.account.amazon.com/ap/oa |
Far East (FE) | https://apac.account.amazon.com/ap/oa |
- API调用查询参数:
参数名 | 描述 |
---|---|
client_id | The Client ID value that you retrieved in the prerequisites section of this document. |
scope | The OAuth 2.0 permission scope used to limit the application's access to an advertiser's account. For the Sponsored Brands, Sponsored Display, and Sponsored Products APIs, set scope to cpc_advertising:campaign_management .For the Data Provider API, set scope to advertising::audiences .For the DSP API, set scope to advertising::campaign_management . |
response_type | The type of response. Always set to code . |
redirect_uri | The value from the Allowed Return URLs field, retrieved in the prerequisites section of this document. |
- 例如,要在North America (NA) 区域生成授权码,请用您的值替换以下URL中的值:
https://www.amazon.com/ap/oa?client_id=YOUR_LWA_CLIENT_ID&scope=cpc_advertising:campaign_management&response_type=code&redirect_uri=YOUR_RETURN_URL
- 接下来,将带有替换值的URL粘贴到浏览器的地址窗口中,然后导航到该URL。
步骤2:登录
- 导航到URL后,系统将提示您登录。
- 对于除数据提供者API以外的所有广告API,请在登录亚马逊时使用您用于注册应用程序的帐户登录。
- 对于数据提供者API,请从数据提供者API的其他设置步骤中回忆起,您为每个区域创建了单独的DSP帐户。使用与步骤1中使用的URL相同的区域使用DSP帐户登录。
步骤3:授予应用程序访问权限
- 登录后,您将被重定向到同意书:
- 要授予应用程序访问Amazon Advertising的权限,请选择Allow。要拒绝应用程序访问Amazon Advertising,请选择Cancel。
步骤4:获取授权码
- 如果您在步骤3中选择了允许,则将浏览器重定向到与步骤1中使用的URL相同区域的Amazon网站。
- 从浏览器的地址栏中选择并复制URL字符串。将URL字符串粘贴到文本编辑器中。记下代码查询参数的值。代码查询参数是在下一步中与授权令牌和刷新令牌交换的授权代码
- 例如,浏览器地址栏中的URL字符串将类似于:
https://www.amazon.com/?code=xxxxxxxxxxxxxxxxxxx&scope=cpc_advertising%3Acampaign_management
步骤5:调用授权URL以请求授权和刷新令牌
- 要检索授权和刷新令牌,请选择与步骤1中使用的URL相同区域的授权URL。URL为:
区域 | Authorization URL |
---|---|
North America (NA) | https://api.amazon.com/auth/o2/token |
Europe (EU) | https://api.amazon.co.uk/auth/o2/token |
Far East (FE) | https://api.amazon.co.jp/auth/o2/token |
接下来,构造API调用以检索授权和刷新令牌。该调用具有以下查询参数:
参数名称 | 描述 |
---|---|
grant_type | Must be authorization_code . |
code | The authorization code retrieved in step 4. |
redirect_uri | One of the values from the consent privacy notice URL field. Recall that you retrieved this value in the prerequisites section of this document. |
client_id | The Client ID value that you retrieved in the prerequisites section of this document. |
client_secret | The Client Secret value that you retrieved in the prerequisites section of this document. |
- API调用需要以下标头:
Content-Type:application/x-www-form-urlencoded
charset=UTF-8
- 例如,要使用诸如cURL之类的工具检索授权和刷新令牌,请在以下请求中替换您的值:
curl \
-X POST \
-H "Content-Type:application/x-www-form-urlencoded;charset=UTF-8" \
--data "grant_type=authorization_code&code=AUTH_CODE&redirect_uri=YOUR_RETURN_URL&client_id=YOUR_CLIENT_ID&client_secret=YOUR_SECRET_KEY" \
https://api.amazon.com/auth/o2/token
此请求的响应是具有以下结构的JSON对象:
字段名 | 描述 |
---|---|
access_token | The authorization token. |
refresh_token | The refresh token. |
token_type | The type of OAuth 2.0 token. Always set to bearer . |
expires_in | The length of time until the authorization token expires, in seconds. |
- 例如:
{
"access_token": "",
"refresh_token": "",
"token_type": "bearer",
"expires_in": 3600
}
- 请注意,授权令牌的有效期为60分钟,并且过期后必须使用刷新令牌进行刷新。刷新令牌不会过期。
- 如下使用刷新令牌,将您的值替换为以下请求:
curl \
-X POST \
-H "Content-Type:application/x-www-form-urlencoded;charset=UTF-8" \
--data "grant_type=refresh_token&client_id=YOUR_CLIENT_ID&refresh_token=YOUR_REFRESH_TOKEN&client_secret=YOUR_CLIENT_SECRET" \
https://api.amazon.com/auth/o2/token
- 此请求的响应与上面的响应相同。