本節包含 Google Play Developer API 專用的操作說明。詳情請參閱完整的 OAuth2 說明文件。
初始設定
存取 Google Play Android Developer API 時,系統會使用 OAuth 2.0 網頁伺服器流程進行驗證。您必須先設定 API 控制台專案、建立用戶端 ID,並產生重新整理權杖,才能使用 API。
建立 API 控制台專案
- 前往 API 控制台,並使用 Google Play 管理中心帳戶登入。
- 選取「建立專案」。
- 前往左側導覽面板中的「服務」。
- 開啟 Google Play Android Developer API。
- 接受《服務條款》。
- 前往左側導覽面板中的「API Access」(API 存取權)。
- 選取「建立 OAuth 2.0 用戶端 ID」。
- 在第一頁中,您需要填寫產品名稱,但不需要提供標誌。請注意,產品名稱不會向使用者顯示。
- 在第二頁選取網路應用程式,然後設定重新導向 URI 和 JavaScript 來源。這兩項設定稍後都可以變更。
- 選取「建立用戶端 ID」。
產生更新權杖
-
使用 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=...
- 系統出現提示時,選取「允許存取」。
-
瀏覽器會重新導向至您的重新導向 URI,並附上
code
參數,看起來會類似4/eWdxD7b-YSQ5CNNb-c2iI83KQx19.wp6198ti5Zc7dJ3UXOl0T3aRLxQmbwI
。 -
將此程式碼傳送至
https://accounts.google.com/o/oauth2/token
,並設定下列欄位,即可換取存取和更新權杖組: 成功的回應會包含 JSON 格式的權杖: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>
{ "access_token" : "ya29.ZStBkRnGyZ2mUYOLgls7QVBxOg82XhBCFo8UIT5gM", "token_type" : "Bearer", "expires_in" : 3600, "refresh_token" : "1/zaaHNytlC3SEBX7F2cfrHcqJEa3KoAHYeXES6nmho" }
存取 API
產生用戶端憑證和重新整理權杖後,伺服器就能存取 API,不必主動登入或人為介入。
使用存取權杖
伺服器可以在要求的 Authorization
標頭中傳遞存取權杖,藉此呼叫 API:
Authorization: Bearer oauth2-token
使用更新權杖
每個存取權杖的效期都很短,目前的存取權杖過期後,伺服器必須使用更新權杖取得新的權杖。如要執行這項操作,請將 POST 要求傳送至 https://accounts.google.com/o/oauth2/token
,並設定下列欄位:
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 帳戶。