OAuth 電腦和網頁應用程式流程

本指南將逐步引導您使用您自己的憑證和桌面流程網頁流程,設定 OAuth2 以存取 API。這些步驟只需執行一次,除非您撤銷、刪除或變更 OAuth2 憑證的允許範圍。

建立 OAuth2 憑證

  1. 按照步驟設定 Google Ads API 的 Google API 控制台專案

  2. 記下用戶端 ID密鑰,再回到這個頁面。

設定用戶端程式庫

  1. 在終端機中執行 GenerateUserCredentials 範例,系統會提示您輸入 OAuth2 用戶端 ID 和密鑰。

    此為互動式範例,會要求您提供輸入內容

    php GenerateUserCredentials.php
    
  2. 本例會提示您造訪某個網址,您必須允許 OAuth2 憑證代您存取 Google Ads 帳戶。

    Paste this URL in your browser:
    https://accounts.google.com/o/oauth2/v2/auth?response_type=code&access_type=offline&client_id=...
    

    在私人瀏覽器工作階段或無痕視窗中前往該網址。 登入您用來存取 Google Ads 的 Google 帳戶。按一下 OAuth2 同意畫面上的「Continue」

    同意畫面

  3. 接著,系統會將您導向一個頁面,顯示授權成功的訊息。

    已擷取更新權杖。請查看控制台輸出內容,取得進一步操作說明。

  4. 返回先前執行範例的控制台。這個範例應已完成,並顯示更新憑證和一些操作說明,後面接著設定用戶端程式庫所需的屬性:

    Your refresh token is: 1/Yw**********
    Copy the text below into a file named "google_ads_php.ini" in your home directory, and replace "INSERT_DEVELOPER_TOKEN_HERE" with your developer token:
    
    [GOOGLE_ADS]
    developerToken = "INSERT_DEVELOPER_TOKEN_HERE"
    [OAUTH2]
    Copy the following lines to your 'google_ads_php.ini' file:
    clientId = "***********************apps.googleusercontent.com"
    clientSecret = "****"
    refreshToken = "****"