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

本指南將逐步引導您設定 OAuth 2.0,以便透過自己的憑證和電腦流程網頁流程存取 API。除非您撤銷、刪除或需要變更 OAuth 2.0 憑證的允許範圍,否則只需執行一次這些步驟。

建立 OAuth 2.0 憑證

  1. 請按照這篇文章的步驟,為 Google Ads API 設定 Google API 控制台專案。

  2. 記下「用戶端 ID」和「用戶端密碼」,然後返回本頁。

設定用戶端程式庫

  1. 在終端機中執行 GenerateUserCredentials 程式碼範例,系統會提示您輸入 OAuth 2.0 用戶端 ID 和用戶端密碼。

    php GenerateUserCredentials.php
    
  2. 程式碼範例會提示您前往網址,並要求您授權應用程式代您存取 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 帳戶登入。通常,這是 Google Ads 管理員帳戶的登入電子郵件地址,其中包含您需要管理的所有帳戶,以及帳戶階層。在 OAuth 2.0 同意畫面中按一下「繼續」

    OAuth 2.0 同意畫面

    系統會將您導向至顯示授權成功的訊息的頁面。

    Your refresh token has been fetched. Check the console output for further
    instructions.
    
  3. 返回執行程式碼範例的控制台。您會看到程式碼範例已完成,並顯示重新整理權杖和一些操作說明,接著是您需要遵循的操作說明,才能設定用戶端程式庫:

    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 = "****"