チュートリアル:Python SDKを使用してSP-API呼び出しを自動化する
Amazonアカウントでログイン(LWA)トークンの交換と認証により、SP-API呼び出しを自動化します。
このチュートリアルでは、Amazonアカウントでログイン(LWA)トークンの交換と認証を使用してPython SDKを生成し、アプリケーションをシームレスに構築するために必要なすべての詳細について説明します。Python SDKの構築に必要な前提条件について学び、注文用のSelling Partner APIとSwagger Code Generatorを使用した例も紹介します。
このSDKを使用すると、商品情報へのアクセス、注文の管理、出荷の処理など、Amazonマーケットプレイスの機能をアプリケーションに統合できます。
チュートリアル
次のチュートリアルにより、独自のPython SDKを設定してSP-API呼び出しを自動化できるようになります。
前提条件
このチュートリアルを完了するには、次の前提条件が必要です。
- ドラフトまたは公開状態のハイブリッドアプリまたはSP-APIアプリ
- 統合開発環境(IDE)ソフトウェア
- Python(バージョン 3.6以降)
swagger-codegen-cli-2.3
(以降)このツールは、SP-APIのSwagger定義からPythonクライアントコードを生成するために使用します。
Before your application can connect to the Selling Partner API, you must register it, and it must be authorized by a selling partner. If you do not have a hybrid or SP-API app, follow the steps to register as a developer, register your application, and Authorizing Selling Partner API applications. Then, return to this tutorial.
次に、チュートリアル用のワークスペースを設定します。
ステップ1. ワークスペースをセットアップします。
-
ローカルドライブに、このプロジェクト用のディレクトリを作成し、
SPAPI_Python_SDK
という名前を付けて新しいディレクトリに移動します。 -
Clone the client repo.
-
以下のツールをダウンロードしてください。
- IDE software (this walkthrough uses Visual Studio IDE on Windows OS)
- Python (version 3.6 or later). You can download this software from python.org.
-
ターミナルで次のコマンドを実行してSwagger Code Jarをダウンロードします。
`wget https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.13/swagger-codegen-cli-2.4.13.jar -O swagger-codegen-cli.jar` -
swagger-codegen-cli.jar
をローカルディレクトリC:\\SPAPI_Python_SDK
にコピーします。 -
ターミナルで次のコマンドを実行し、Python
backoff
ライブラリを環境内にインストールします。pip install backoff -
In GitHub, go to
https://github.com/amzn/selling-partner-api-models/tree/main/models
and run the following command to clone theselling-partner-api-models
repository to your local directoryC:\\SPAPI_Python_SDK
.git clone https://github.com/amzn/selling-partner-api-models
必要な設定が完了したので、次のステップでは、ローカルディレクトリC:\\SPAPI_Python_SDK
にダウンロードした認証クラスと承認クラスを使用してPython SDKを生成します。
ステップ2. Swagger定義からPythonクライアントを生成します。
-
Locate the Swagger JSON file for the SP-API API model you want to use (for example, Orders API) from your local directory
C:\\SPAPI_Python_SDK
. -
ターミナルで次のコマンドを実行して、クライアントコードを生成します。パスとAPIモデルは必ず設定に置き換えてください。
java -jar /[path_to_swagger_jar]/swagger-codegen-cli.jar generate -l python -t /[path_to_mustach_resources]/resources/ -D packageName=swagger_client -o /[path_to_client_folder]/client/[SP-API_NAME] -i /[path_to_model_folder]/models/[SP-API_NAME]/SP-API.json
Pythonクライアントを生成したので、認証モデルを統合する必要があります。
ステップ3. 認証モジュールを統合します。
- SDKをダウンロードした
C:\\SPAPI_Python_SDK
ディレクトリのauth
とspapi
クライアントコードフォルダーを特定します。 - Pythonファイル
spapiclient.py
とLwaRequest.py
のパスを更新します。各ファイルには、更新内容についての説明が含まれています。
認証を設定したら、Python SDKパッケージをセットアップする準備が整います。
ステップ4. Python SDKパッケージをセットアップします。
- SDKを生成した
C:\\SPAPI_Python_SDK
ディレクトリに移動します。 - 次のコードを使用して
setup.py
ファイルを作成してください。このファイルはSDKのパッケージ化に必要です。この例の情報は、パッケージと依存関係の情報に置き換えてください。
from setuptools import setup, find_packages
setup(
name='SellingPartnerAPIAuthAndAuthPython', # Replace with your package's name
version='1.0.0', # Replace with your package's version
package_dir={'': 'src'}, # Replace 'src' as necessary
packages=find_packages(where='src'),
install_requires=[line.strip() for line in open("requirements.txt", "r")],
description='A Python SDK for Amazon Selling Partner API',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
url='TBD'
)
Python SDKをセットアップしたら、注文APIのインスタンスを作成する準備が整います。
ステップ5. 注文APIのインスタンスを作成し、オペレーションを呼び出します。
以下は、Python SDKと注文APIを使用してgetOrders
リクエストを作成する方法の例です。情報を使用してコードを更新し、ターミナルでコードを実行します。
if __name__ == "__main__":
from auth.credentials import SPAPIConfig
config = SPAPIConfig(
client_id="Your client-id",
client_secret="Your client-secret",
refresh_token="Your refresh-token",
region="NA", # Possible values NA, EU, FE, and SANDBOX
scope = None # Required for grant_type='client_credentials' ; Possible values "sellingpartnerapi::notifications" and "sellingpartnerapi::migration"
)
from spapi.spapiclient import SPAPIClient
# Create the API Client
print("Config and client initialized...")
api_client = SPAPIClient(config)
marketplace_ids = ["ATVPDKIKX0DER"]
created_after = "2024-01-19T00:00:00"
orders_api = api_client.get_api_client('OrdersV0Api')
orders_response = orders_api.get_orders(marketplace_ids=marketplace_ids, created_after=created_after)
print("Orders API Response:")
print(orders_response)
注意
このファイルには機密情報が含まれているため、絶対にバージョン管理システムにコミットしないでください。これらのLWA認証情報は、暗号化形式で安全に保管してください。
ステータスコード200は、API呼び出しが成功したという意味です。
ステップ6. 生成されたPython SDKを使用してSelling Partner APIに接続します。
ターミナルで次のコマンドを実行して、SDKをローカルで構築してインストールします。
python3 setup.py sdist bdist_wheel
pip install dist/{YourPackageName}-1.0.0-py3-none-any.whl
ターミナルで次のテストスクリプトを実行し、Python SDKをテストします。
python test.py
ステータスコード200は、API呼び出しが成功したという意味です。
結論
このチュートリアルでは、SP-API SDK for Pythonを使用してSP-API呼び出しを自動化する方法を学びました。ウォークスルーでは、ワークスペースの設定方法、Python SDK for Selling Partner APIの生成方法、注文APIへの接続方法、最初のAPI呼び出しの方法を学びました。
Updated 23 days ago