对接亚马逊 SP-API(Amazon Selling Partner API) 第五章:Reports 模块
目錄
1. 前提概要
1.1. 官方文檔
1.1.1. ReportType Values
1.1.2.?ReportType 請求體
1.1.3. 請求教程
1.2. 下面內(nèi)容會以 ALL_ORDERS 為案例
2. 流程對比
2.1. MWS 流程
2.2. SP-API 流程
3. HTTP 對接
官方案例(Java & C #)
Step 1. Request a report
3.1.1. Headers
3.1.2. Body
3.1.3. 完整請求 URL
Step 2. Confirm report processing has completed
Step 3. Retrieve the report
4. SDK 對接
4.1. 下載對應(yīng)的 SDK
4.1.1. model.json
4.1.2. 引入 aa 庫
4.1.3. 引入 documents-helper? 庫
4.2. 創(chuàng)建 getReportsApi()
4.3. 創(chuàng)建報告(獲取 reportId)
Bug 1. Bad Request
Bug 1.1.?One or more required parameters missing
Bug 1.2. Report rejected for this client. Some of the marketplaces are not allowed for this Report
?Bug 2.?io.swagger.client.ApiException: Forbidden
Bug 2.1. 無效帳號?
Bug 2.2. 傳參錯誤
Bug 2.3. 枚舉不對 或者 不支持的區(qū)域。
4.4. 檢查報告申請狀態(tài)(獲取 reportDocumentId)
Bug 1.?CANCELLED
4.5. 獲取下載鏈接
4.6. 使用?DownloadExample.java 下載 report
Bug 1. java.lang.NoClassDefFoundError: com/google/common/base/Preconditions
Bug 2. java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils?
Bug 3. java.lang.IllegalArgumentException: No enum constant com.amazon.spapi.documents.CompressionAlgorithm.AES?
Bug 3.1. 亂碼(峰兄提供)
Bug 4.?com.amazon.spapi.documents.exception.CryptoException: java.security.InvalidKeyException: Illegal key size
總結(jié)
這一章我將會展示完整的流程,其他模塊將不再重復(fù)相似的步驟了。
1. 前提概要
1.1. 官方文檔
1.1.1. ReportType Values
這部分介紹了有哪些枚舉類。
https://github.com/amzn/selling-partner-api-docs/blob/main/references/reports-api/reportType_string_array_values.md
1.1.2.?ReportType 請求體
這部分介紹了 HTTP 請求的 body 的參數(shù)有哪些。
https://github.com/amzn/selling-partner-api-docs/blob/main/references/reports-api/reports_2020-09-04.md
1.1.3. 請求教程
https://github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/use-case-guides/reports-api-use-case-guide/reports-api-use-case-guide-2020-09-04.md
1.2. 下面內(nèi)容會以 ALL_ORDERS 為案例
2. 流程對比
2.1. MWS 流程
RequestReport ==> GetReportRequestList ==>?GetReport
2.2. SP-API 流程
createReport ==>?getReport ==>?getReportDocument?==>?Download and decrypt
官網(wǎng)介紹:https://github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/use-case-guides/reports-api-use-case-guide/reports-api-use-case-guide-2020-09-04.md#tutorial-request-and-retrieve-a-report
簡單來說就是:
1. createReport() 獲取 reportId
2. 通過 reportId 調(diào)用 getReport() 檢查報告狀態(tài)。DONE 的情況會返回 reportDocumentId
3. 通過 reportDocumentId 調(diào)用 getReportDocument() 獲取下載 url 和 文件解碼
3. HTTP 對接
官方案例(Java & C #)
https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-examples-using-sdks.html#sig-v4-examples-using-sdk-java
AWSS3SigV4JavaSamples
這個 demo 對拼接 http 請求非常好用。
Step 1. Request a report
3.1.1. Headers
Authorization: AWS4-HMAC-SHA256 Credential=ABCXXXXXXX/20210423/us-east-1/execute-api/aws4_request, SignedHeaders=host;user-agent;x-amz-access-token, Signature=5d672d79c15b13162d9279b0855cfba6789a8edb4c82c400e06b5924aEXAMPLE
user-agent: My Selling Tool/2.0 (Language=Java/1.8.0.221;Platform=Windows/10)
x-amz-access-token=Atza|IQEBLjAsAhRmHjNgHpi0U-Dme37rR6CuUpSREXAMPLE
x-amz-date: 20210423T123400Z
| x-amz-date | The date and time of your request. |
| user-agent | Your application name and version number, platform, and programming language. These help Amazon diagnose and fix problems you might encounter with the service. See?Include a User-Agent header in all?requests. 應(yīng)用程序名稱和版本號、平臺和編程語言 |
3.1.1.1.?x-amz-access-token
access token 有效期為一個小時。我們可以通過 refresh token 獲取最新的 access token。
cURL
curl --location --request POST 'https://api.amazon.com/auth/o2/token' \
--header 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8' \
--data-raw 'grant_type=refresh_token&refresh_token={refresh_token}&client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}'
3.1.1.2.?x-amz-security-token
使用 AWS Security Token Service (AWS STS) 提供的臨時安全證書對請求進(jìn)行簽名。其過程與使用長期憑證相同,但在您添加簽名信息到查詢字符串時,您必須為安全令牌添加額外的查詢參數(shù)。參數(shù)名稱為?X-Amz-Security-Token,參數(shù)值為 URI 編碼的會話令牌(在您獲取臨時安全憑證時從 AWS STS 收到的字符串)。
3.1.1.3. Authorization
對接亞馬遜 SP-API(Selling Partner API) 第四章:簽名
3.1.2. Body
| reportOptions | Additional information passed to reports. This varies by report type. Type:?ReportOptions | No |
| reportType | The report type. For more information, see?reportType values. Type: string | Yes |
| dataStartTime | The start of a date and time range, in ISO 8601 date time format, used for selecting the data to report. The default is now. The value must be prior to or equal to the current date and time. Not all report types make use of this. Type: string (date-time) | No |
| dataEndTime | The end of a date and time range, in ISO 8601 date time format, used for selecting the data to report. The default is now. The value must be prior to or equal to the current date and time. Not all report types make use of this. Type: string (date-time) | No |
| marketplaceIds | A list of marketplace identifiers. The report document's contents will contain data for all of the specified marketplaces, unless the report type indicates otherwise. Type: < string > array | Yes |
{
? "reportType": "GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL",
? "dataStartTime": "2021-04-20T00:00:00.000Z",
? "marketplaceIds": [
? ? "A1PA6795UKMFR9",
? ? "ATVPDKIKX0DER"
? ]
}
3.1.3. 完整請求 URL
cUrl
curl --location --request POST 'https://sellingpartnerapi-na.amazon.com/reports/2020-09-04/reports' \
--header 'Authorization: Authorization' \
--header 'user-agent: My Selling Tool/2.0 (Language=Java/1.8.0.221;Platform=Windows/10)' \
--header 'x-amz-access-token: Atza|XXX' \
--header 'x-amz-date: 20210423T123400' \
--header 'Content-Type: application/json' \
--data-raw '{
??"reportType": "GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL",
??"dataStartTime": "2021-04-20T00:00:00.000Z",
??"marketplaceIds": [
????"A1PA6795UKMFR9",
????"ATVPDKIKX0DER"
??]
}'
Step 2. Confirm report processing has completed
Step 3. Retrieve the report
4. SDK 對接
?前提概要:
使用 sellers-api 模塊的?getMarketplaceParticipations(),獲取帳號所啟用的站點(diǎn)(因?yàn)閞eports-api 的 marketplace 是必填項(xiàng))。否則會提示站點(diǎn)不可用。
4.1. 下載對應(yīng)的 SDK
對接亞馬遜 SP-API(Selling Partner API) 第三章:對接 SDK
4.1.1. model.json
暫時使用 2020-09-04 版本。
4.1.2. 引入 aa 庫
4.1.3. 引入 documents-helper? 庫
官方提供了 Java 解壓的 demo(DownloadExample.java)。不需要的可以不引入 documents-helper? 庫。
Bug 1.?打包的時候會發(fā)現(xiàn) test 包下面報錯。
解決方法
打包的時候把測試類跳過就好。(解決不了出問題,就把出問題的地方解決掉)
4.2. 創(chuàng)建 getReportsApi()
public static ReportsApi getReportsApi() {AWSAuthenticationCredentials awsAuthenticationCredentials;AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider;LWAAuthorizationCredentials lwaAuthorizationCredentials;awsAuthenticationCredentials = AWSAuthenticationCredentials.builder().accessKeyId(USER_ACCESS_KEY_ID).secretKey(USER_SECRET_ACCESS_KEY).region(REGION).build();awsAuthenticationCredentialsProvider = AWSAuthenticationCredentialsProvider.builder().roleArn(ROLE_ARN).roleSessionName(ROLE_SESSION_NAME).build();lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder().clientId(APP_CLIENT_ID).clientSecret(APP_CLIENT_SECRET).refreshToken(REFRESH_TOKEN).endpoint(LWA_ENDPOINT).build();ReportsApi reportsApi = new ReportsApi.Builder().awsAuthenticationCredentials(awsAuthenticationCredentials).lwaAuthorizationCredentials(lwaAuthorizationCredentials).awsAuthenticationCredentialsProvider(awsAuthenticationCredentialsProvider).endpoint(REGION_ENDPOINT).build();if (null == reportsApi) {throw new RuntimeException();}return reportsApi;}4.3. 創(chuàng)建報告(獲取 reportId)
?請求限制:
?最大請求為10個點(diǎn)數(shù),以每秒 0.0222 個恢復(fù),即 45 秒恢復(fù)一個請求點(diǎn)數(shù)。
對比 MWS?最大限額小了,恢復(fù)速度快了。
4.3. 1. Bad Request
錯誤提示不太友好。這個我查了半天才發(fā)現(xiàn) Body 中 Marketplace ID 是個必填項(xiàng)。
Bug 1.?One or more required parameters missing
{"errors": [{"code": "InvalidInput","message": "One or more required parameters missing","details": "marketplaceIds;"}] }解決辦法
參考文檔把必填項(xiàng)補(bǔ)充進(jìn)去就好了。
Bug 2. Report rejected for this client. Some of the marketplaces are not allowed for this Report
{"errors": [{"code": "InvalidInput","message": "Report rejected for this client. Some of the marketplaces are not allowed for this Report","details": ""}] }?解決辦法
1. 確定自己的 marketplaceId 沒有填錯。
2. 去文檔找這個 report type 是否支持當(dāng)前站點(diǎn)。
?問題倒是找到了。但是其他北美的帳號可以正常申請這個類型.. 很迷 - -。
最后在 Github issues 發(fā)現(xiàn)答案。
https://github.com/amzn/selling-partner-api-docs/issues/911
4.3.2.?io.swagger.client.ApiException: Forbidden
備注:2021-06-30 之前,SP-API 麻煩使用 2020-09-04 這個版本。?
Bug 1. 無效帳號?
?我們首先確定一點(diǎn),這個帳號是有效的。登錄亞馬遜后臺,你會發(fā)現(xiàn)首頁就顯示這個帳號掛掉了。
備注:當(dāng)這個帳號被凍結(jié)了,我們依然是可以正常進(jìn)入后臺且可以拿到 refresh_token 的,也可以通過 refresh_token 拿到 access_token。只是無法再進(jìn)行下一步的 Api 操作了。
Bug 2. 傳參錯誤
解決辦法
找到你報錯的地方,打上斷點(diǎn)。在異常的?Message 有寫明是因?yàn)槭裁磮箦e。
Bug 3. 枚舉不對 或者 不支持的區(qū)域。
{"errors": [{"code": "Unauthorized","message": "Access to the resource is forbidden","details": ""}] }??由于我請求的報告是 Amazon Fulfilled Shipments。并且確信授權(quán)了這個模塊。所以權(quán)限是沒問題的。
枚舉類:因?yàn)槲野l(fā)現(xiàn) MWS 和 SP-API 的枚舉類都是一樣的,只是前后的 _ 取消了。所以我理所當(dāng)然的使用了:GET_AMAZON_FULFILLED_SHIPMENTS_DATA
然后跑去官網(wǎng)查看。你就會發(fā)現(xiàn),正確的枚舉類長這樣:GET_AMAZON_FULFILLED_SHIPMENTS_DATA_GENERAL
官方解釋?
4.4. 檢查報告申請狀態(tài)(獲取 reportDocumentId)
?Processing Statuses
Bug 1.?CANCELLED
請求了大量的報告你會發(fā)現(xiàn),很多報告會出現(xiàn) Cancelled 狀態(tài)。以我多年的 MWS 經(jīng)驗(yàn),我斷定是我發(fā)送請求太頻繁了導(dǎo)致的。然后我就不斷的重新發(fā)起請求.. 不斷的 Cancelled。
最后我跑去后臺查,發(fā)現(xiàn)沒數(shù)據(jù)也會顯示 Cancelled。這就很尷尬了。
去官方文檔查詢,你會發(fā)現(xiàn)?
大概的意思是:有兩種可能會出現(xiàn) Cancelled 狀態(tài),1. 自己取消。2. 沒數(shù)據(jù)。
?我還是懷念 MWS 的 _DONE_NO_DATA_,簡單明了。而且從 MWS 升級過來,有好多坑都是經(jīng)驗(yàn)式 Bug..
(猜想)狀態(tài)變更:SP-API 請求數(shù)據(jù)太頻繁的報告狀態(tài)是 FATAL。
4.5. 獲取下載鏈接
4.6. 使用?DownloadExample.java 下載 report
Bug 1. java.lang.NoClassDefFoundError: com/google/common/base/Preconditions
解決方法
<!-- https://mvnrepository.com/artifact/com.google.guava/guava --><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>30.1.1-jre</version></dependency>Bug 2. java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils?
解決方法
<!-- https://mvnrepository.com/artifact/commons-io/commons-io --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency>Bug 3. java.lang.IllegalArgumentException: No enum constant com.amazon.spapi.documents.CompressionAlgorithm.AES?
?
CompressionAlgorithm.java 還真沒有.. 就很尷尬了..
解決辦法
構(gòu)建?DownloadSpecification 的時候,不加這個算法。
Bug 3.1. 亂碼(峰兄提供)
情景:
某些報告下載下來是正常的,某些報告是亂碼。
?原因:
1)、正常報告 Response:
2)、亂碼報告 Response:
導(dǎo)致亂碼的原因是我們在 BUG 3 中把算法去掉了。而數(shù)據(jù)量如果比較大的情況,亞馬遜會返回一個 GZIP 文件,而我們又沒解析。
解決辦法
Bug 4.?com.amazon.spapi.documents.exception.CryptoException: java.security.InvalidKeyException: Illegal key size
原因:JAVA默認(rèn)支持AES 128 Bit 的key, 如果你計劃使用 192 Bit 或者 256 Bit key, java complier 會拋出 Illegal key size Exception
解決辦法
Plan A
替換 Policy 文件(選擇對應(yīng) Java 版本下載)(需要注冊個 Oracle 帳號)
https://www.oracle.com/java/technologies/javase-jce-all-downloads.html
替換 JDK 與 JRE 下兩個 jar 包:local_policy.jar 和 US_export_policy.jar
JDK 對應(yīng) jar 包路徑:%JAVA_HOME%\jre\lib\security
JRE 對應(yīng) jar 包路徑:%JAVA_JRE%\lib\security
Plan B
升級 Java 版本
總結(jié)
1. SDK 比 自己發(fā)送 HTTP 請求要簡單一些。
總結(jié)
以上是生活随笔為你收集整理的对接亚马逊 SP-API(Amazon Selling Partner API) 第五章:Reports 模块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2016年5月热门IT职位的推荐
- 下一篇: 小学计算机考核,小学信息技术学科考核评价