本節提供 Google Play Developer API 的專屬操作說明。詳情請參閱完整的 OAuth2 說明文件。
初始設定
系統會透過 OAuth 2.0 網路伺服器流程來驗證 Google Play Android Developer API 的存取權。您必須先設定 API 控制台專案、建立用戶端 ID 並產生更新權杖,才能使用 API。
建立 API 控制台專案
- 前往 API 控制台,並使用您的 Google Play 管理中心帳戶登入。
- 選取「建立專案」。
- 前往左側導覽面板中的 [服務]。
- 開啟 Google Play Android Developer API。
- 請接受《服務條款》。
- 前往左側導覽面板中的「API Access」(API 存取權)。
- 選取「Create an OAuth 2.0 client ID」(建立 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=...
- 在系統提示時,選取「允許存取」。
-
瀏覽器會使用
code
參數將瀏覽器重新導向至重新導向 URI,參數類似於4/eWdxD7b-YSQ5CNNb-c2iI83KQx19.wp6198ti5Zc7dJ3UXOl0T3aRLxQmbwI
。 -
如要交換這個程式碼以取得存取權和更新權杖組合,請傳送 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
標頭中傳遞存取權杖,藉此呼叫 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 帳戶的有效登入。