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