日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

对接亚马逊 SP-API(Amazon Selling Partner API) 第五章:Reports 模块

發布時間:2023/12/16 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 对接亚马逊 SP-API(Amazon Selling Partner API) 第五章:Reports 模块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

1. 前提概要

1.1. 官方文檔

1.1.1. ReportType Values

1.1.2.?ReportType 請求體

1.1.3. 請求教程

1.2. 下面內容會以 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. 下載對應的 SDK

4.1.1. model.json

4.1.2. 引入 aa 庫

4.1.3. 引入 documents-helper? 庫

4.2. 創建 getReportsApi()

4.3. 創建報告(獲取 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. 枚舉不對 或者 不支持的區域。

4.4. 檢查報告申請狀態(獲取 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

總結


這一章我將會展示完整的流程,其他模塊將不再重復相似的步驟了。

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 的參數有哪些。

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. 下面內容會以 ALL_ORDERS 為案例

2. 流程對比

2.1. MWS 流程

RequestReport ==> GetReportRequestList ==>?GetReport

2.2. SP-API 流程

createReport ==>?getReport ==>?getReportDocument?==>?Download and decrypt

官網介紹: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 調用 getReport() 檢查報告狀態。DONE 的情況會返回 reportDocumentId

3. 通過 reportDocumentId 調用 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

NameDescription
x-amz-dateThe 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.

應用程序名稱和版本號、平臺和編程語言

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) 提供的臨時安全證書對請求進行簽名。其過程與使用長期憑證相同,但在您添加簽名信息到查詢字符串時,您必須為安全令牌添加額外的查詢參數。參數名稱為?X-Amz-Security-Token,參數值為 URI 編碼的會話令牌(在您獲取臨時安全憑證時從 AWS STS 收到的字符串)。

3.1.1.3. Authorization

對接亞馬遜 SP-API(Selling Partner API) 第四章:簽名

3.1.2. Body

NameDescriptionRequired
reportOptionsAdditional information passed to reports. This varies by report type.

Type:?ReportOptions

No
reportTypeThe report type. For more information, see?reportType values.

Type: string

Yes
dataStartTimeThe 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
dataEndTimeThe 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
marketplaceIdsA 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(),獲取帳號所啟用的站點(因為reports-api 的 marketplace 是必填項)。否則會提示站點不可用。

4.1. 下載對應的 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.?打包的時候會發現 test 包下面報錯。

解決方法

打包的時候把測試類跳過就好。(解決不了出問題,就把出問題的地方解決掉)

4.2. 創建 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. 創建報告(獲取 reportId)

?請求限制:

?最大請求為10個點數,以每秒 0.0222 個恢復,即 45 秒恢復一個請求點數。

對比 MWS?最大限額小了,恢復速度快了。

4.3. 1. Bad Request

錯誤提示不太友好。這個我查了半天才發現 Body 中 Marketplace ID 是個必填項。

Bug 1.?One or more required parameters missing

{"errors": [{"code": "InvalidInput","message": "One or more required parameters missing","details": "marketplaceIds;"}] }

解決辦法

參考文檔把必填項補充進去就好了。

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 是否支持當前站點。

?問題倒是找到了。但是其他北美的帳號可以正常申請這個類型.. 很迷 - -。

最后在 Github issues 發現答案。

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. 無效帳號?

?我們首先確定一點,這個帳號是有效的。登錄亞馬遜后臺,你會發現首頁就顯示這個帳號掛掉了。

備注:當這個帳號被凍結了,我們依然是可以正常進入后臺且可以拿到 refresh_token 的,也可以通過 refresh_token 拿到 access_token。只是無法再進行下一步的 Api 操作了。

Bug 2. 傳參錯誤

解決辦法

找到你報錯的地方,打上斷點。在異常的?Message 有寫明是因為什么報錯。

Bug 3. 枚舉不對 或者 不支持的區域。

{"errors": [{"code": "Unauthorized","message": "Access to the resource is forbidden","details": ""}] }

??由于我請求的報告是 Amazon Fulfilled Shipments。并且確信授權了這個模塊。所以權限是沒問題的。

枚舉類:因為我發現 MWS 和 SP-API 的枚舉類都是一樣的,只是前后的 _ 取消了。所以我理所當然的使用了:GET_AMAZON_FULFILLED_SHIPMENTS_DATA

然后跑去官網查看。你就會發現,正確的枚舉類長這樣:GET_AMAZON_FULFILLED_SHIPMENTS_DATA_GENERAL

官方解釋?

4.4. 檢查報告申請狀態(獲取 reportDocumentId)

?Processing Statuses

Bug 1.?CANCELLED

請求了大量的報告你會發現,很多報告會出現 Cancelled 狀態。以我多年的 MWS 經驗,我斷定是我發送請求太頻繁了導致的。然后我就不斷的重新發起請求.. 不斷的 Cancelled。

最后我跑去后臺查,發現沒數據也會顯示 Cancelled。這就很尷尬了。

去官方文檔查詢,你會發現?

大概的意思是:有兩種可能會出現 Cancelled 狀態,1. 自己取消。2. 沒數據。

?我還是懷念 MWS 的 _DONE_NO_DATA_,簡單明了。而且從 MWS 升級過來,有好多坑都是經驗式 Bug..

猜想)狀態變更:SP-API 請求數據太頻繁的報告狀態是 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 還真沒有.. 就很尷尬了..

解決辦法
構建?DownloadSpecification 的時候,不加這個算法。

Bug 3.1. 亂碼(峰兄提供)

情景:

某些報告下載下來是正常的,某些報告是亂碼。

?原因:

1)、正常報告 Response:

2)、亂碼報告 Response:

導致亂碼的原因是我們在 BUG 3 中把算法去掉了。而數據量如果比較大的情況,亞馬遜會返回一個 GZIP 文件,而我們又沒解析。

解決辦法

Bug 4.?com.amazon.spapi.documents.exception.CryptoException: java.security.InvalidKeyException: Illegal key size

原因:JAVA默認支持AES 128 Bit 的key, 如果你計劃使用 192 Bit 或者 256 Bit key, java complier 會拋出 Illegal key size Exception

解決辦法

Plan A
替換 Policy 文件(選擇對應 Java 版本下載)(需要注冊個 Oracle 帳號)

https://www.oracle.com/java/technologies/javase-jce-all-downloads.html

替換 JDK 與 JRE 下兩個 jar 包:local_policy.jar 和 US_export_policy.jar
JDK 對應 jar 包路徑:%JAVA_HOME%\jre\lib\security
JRE 對應 jar 包路徑:%JAVA_JRE%\lib\security
Plan B
升級 Java 版本

總結

1. SDK 比 自己發送 HTTP 請求要簡單一些。

總結

以上是生活随笔為你收集整理的对接亚马逊 SP-API(Amazon Selling Partner API) 第五章:Reports 模块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。