授權

本節包含 Google Play Developer API 的專屬操作說明。詳情請參閱 完整的 OAuth2 說明文件 ,掌握更多詳細資訊。

初始設定

系統會驗證 Google Play Android Developer API 的存取權,並使用 OAuth 2.0 網路伺服器 流程您必須先設定 API 控制台,才能使用這個 API 專案,建立用戶端 ID 並產生更新權杖。

建立 API 控制台專案

  1. 前往 API 控制台並登入 登入 Google Play 管理中心帳戶
  2. 選取「建立專案」
  3. 前往左側導覽面板的「服務」
  4. 啟用 Google Play Android Developer API
  5. 請接受《服務條款》。
  6. 前往左側導覽面板的 [API Access]
  7. 選取「Create an OAuth 2.0 client ID」(建立 OAuth 2.0 用戶端 ID)
    • 您必須在第一頁填寫產品名稱,並設定標誌 不需要。請注意,使用者不會看到產品名稱。
    • 在第二個網頁上,選取網頁應用程式並設定重新導向 URI,然後 JavaScript 來源。這兩項設定日後都可以變更。
  8. 選取「Create client ID」(建立用戶端 ID)

產生更新權杖

  1. 使用 Google Play 管理中心帳戶登入後,前往 存取 URI:
    https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri=...&client_id=...
    敬上
  2. 在系統提示時選取「允許存取」
  3. 瀏覽器會以 code 重新導向至重新導向 URI 參數,與 4/eWdxD7b-YSQ5CNNb-c2iI83KQx19.wp6198ti5Zc7dJ3UXOl0T3aRLxQmbwI 類似。
  4. 請傳送 POST 來交換此代碼以存取存取權和更新權杖組合 要求 https://accounts.google.com/o/oauth2/token,並使用 以下欄位組合:
    grant_type=authorization_code
    code=<the code from the previous step>
    client_id=<the client ID token created in the APIs Console>
    client_secret=<the client secret corresponding to the client ID>
    redirect_uri=<the URI registered with the client ID>
    
    敬上 成功的回應會包含 JSON 格式的權杖:
    {
      "access_token" : "ya29.ZStBkRnGyZ2mUYOLgls7QVBxOg82XhBCFo8UIT5gM",
      "token_type" : "Bearer",
      "expires_in" : 3600,
      "refresh_token" : "1/zaaHNytlC3SEBX7F2cfrHcqJEa3KoAHYeXES6nmho"
    }
    

存取 API

產生用戶端憑證和更新權杖後,您的伺服器 不需要主動登入或人為介入,即可使用 API。

使用存取權杖

伺服器可以藉由傳送 要求的 Authorization 標頭:

Authorization: Bearer oauth2-token

使用更新權杖

每個存取權杖的有效時間有限。目前的存取權杖 過期,伺服器就需要使用更新權杖來取得新的權杖。待辦 也就是向 https://accounts.google.com/o/oauth2/token 傳送 POST 要求,且 下列欄位組合:

grant_type=refresh_token
client_id=<the client ID token created in the APIs Console>
client_secret=<the client secret corresponding to the client ID>
refresh_token=<the refresh token from the previous step>

成功的回應會包含另一個存取權杖:

{
  "access_token" : "ya29.AHES3ZQ_MbZCwac9TBWIbjW5ilJkXvLTeSl530Na2",
  "token_type" : "Bearer",
  "expires_in" : 3600,
}

因此,在未取得更新權杖的情況下,網路伺服器即可持續存取 API 要求有效登入 Google 帳戶。