🌐

Amazon Advertising APIを叩くまで by JS

2021/09/04に公開

はじめに

業務にて Amazon の advertising-api を調べていたのですが、まだまだ日本語記事が出回っておらず、公式の英語ドキュメントのみでした。
私自身苦労したので、ここで一部公式を抜粋しながら認証を通して、実際に API を叩くところまで紹介していきます!!

なお今回は、js を使い、ウェブブラウザからサードパーティとして、認証情報を取得して API を叩きます

Advertising API って?

The Amazon Advertising API lets you programmatically manage your advertising operations.

いわゆる広告操作をプログラムで管理できるということですね!
以下公式ドキュメント
https://advertising.amazon.com/API/docs/en-us/what-is/amazon-advertising-api

API の種類
  • sponsored brand -> 商品検索結果の上部に掲載される広告
  • sponsored display -> 設定したターゲティングに基づきさまざまな場所に表示される広告
  • sponsored product -> 設定したキーワードが検索されると表示される広告
  • Amazon DSP -> Amazon 以外の外部広告

今回は、API を叩くまでが目標なので詳細は、省かせていただきます。
Amazon 広告ついて詳しく知りたい方は、以下の記事からどうぞ!!
【Amazon 広告のキホン!】メリットや種類・仕組みを徹底解説!

セキュリティプロファイルの作成

https://developer.amazon.com/loginwithamazon/console/site/lwa/overview.html

  1. 上記のリンクからセキュリティプロファイルを作成
  2. 作成した、プロファイルの設定ボタンからウェブ設定を選択し、編集をクリック
  3. その後、許可されたオリジンと許可された返信 URL に任意の URL を登録
    今回は、テストケースになるので、どちらともhttp://localhost:3000で登録します。
    なおこの URL は、複数登録が可能です!
  4. クライアント ID とクライアントシークレットをコピー(後程使用)

セキュリティプロファイルの作成方法詳細は、以下のリンクから
https://developer.amazon.com/ja/docs/login-with-amazon/register-web.html

実装

  1. login with amazon ボタンをユーザーがクリック後、同意フォームにリダイレクト
    (ユーザーが認証を許可後、YOUR_RETURN_URLにリダイレクトされる。この時、paramsにはcodeが含まれる)
  2. paramscodeを使い、access_tokenrefresh_tokenを取得
  3. access_tokenを使い、ユーザーのprofileを取得
  4. access_tokenprofileIdを使い、api を叩く

この流れで、認証を打破していきます!

login with amazon ボタンをサイト内に設置
クリック後、以下の URL(同意フォーム)にリダイレクトさせます
https://www.amazon.com/ap/oa?client_id=YOUR_LWA_CLIENT_ID&scope=advertising::campaign_management&response_type=code&redirect_uri=YOUR_RETURN_URL

ユーザーが認証を許可すると以下の状態でYOUR_RETURN_URLにリダイレクトされます。
http://localhost:3000?code=xxxxxxxxxxxxxxxxxxx&scope=cpc_advertising%3Acampaign_management

params の中にaccess_tokenと交換できるcodeが含まれているため、以下のようにaccess_tokenを取得します。

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.co.jp/auth/o2/token
// nodejs
const exchangeAccessToken = async (code) => {
  const response = await fetch("https://api.amazon.co.jp/auth/o2/token", {
    method: "post",
    headers: {
      "Content-Type": "application/json;charset=UTF-8",
    },
    body: JSON.stringify({
      grant_type: "authorization_code",
      code: YOUR_AUTH_CODE,
      redirect_uri: "http://localhost:3000",
      client_id: YOUR_CLIENT_ID,
      client_secret: YOUR_CLIENT_SECRET,
    }),
  })
    .then((res) => res.json())
    .catch((err) => false);
  return response;
};

そのaccess_tokenを使って以下のようにprofileを取得

  curl \
    -X GET \
    -H "Content-Type:application/json" \
    -H "Authorization: Bearer YOUR\_ACCESS\_TOKEN" \
    -H "Amazon-Advertising-API-ClientId: YOUR_CLIENT_ID" \
    https://advertising-api-fe.amazon.com/v2/profiles
// nodejs
const getProfile = async(access_token) => {
  const response = await fetch('https://advertising-api-fe.amazon.com/v2/profiles', {
    method: 'get',
    headers: {
      "Content-Type": "application/json",
      "Authorization": "Bearer " + YOUR_ACCESS_TOKEN,
      "Amazon-Advertising-API-ClientId": YOUR_CLIENT_ID
    }
  }).then(res => res.json()).catch(error => false)
  return response
)}

access_tokenは、1時間のみ有効
refresh_tokenは、対象ユーザーに対して期間関係なく使えるもので、以下のようにaccess_tokenを交換することができます

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

profileIdaccess_tokenを取得できたら、以下のようにすることで API が叩けます

  curl \
    -X GET \
    -H "Content-Type:application/json" \
    -H "Authorization: Bearer YOUR\_ACCESS\_TOKEN" \
    -H "Amazon-Advertising-API-ClientId: YOUR_CLIENT_ID" \
    -H "Amazon-Advertising-API-Scope": YOUR_PROFILE_ID
    https://advertising-api-fe.amazon.com/v2/sp/campaigns
const getSpCampaigns = async () => {
  const response = await fetch(
    "https://advertising-api-fe.amazon.com/v2/sp/campaigns",
    {
      method: "get",
      headers: {
        "Content-Type": "application/json",
        Authorization: "Bearer " + YOUR_ACCESS_TOKEN,
        "Amazon-Advertising-API-ClientId": YOUR_CLIENT_ID,
        "Amazon-Advertising-API-Scope": YOUR_PROFILE_ID,
      },
    }
  )
    .then((res) => res.json())
    .catch((error) => false);
  return response;
};

終わりに

実際に API を叩くまで長いですよね。
私自身まだ駆け出しなので、苦労しましたが、ドキュメントを早く理解して、すぐ実装できるようになりたいです!切実に、、、
https://advertising.amazon.com/API/docs/en-us/what-is/amazon-advertising-api

GitHubで編集を提案

Discussion