###### tags: `Google ads api` `Developer token` `測試環境` # Google ads api developer token ## 使用對象與時機 > 第一次接觸使用Google ads api,需要產生一組測試用的Developer token 與相關測試環境時可以follow此流程 ## 產生/取得Developer token ### 建立Manager account [Google Ads Api Overview](https://developers.google.com/google-ads/api/docs/first-call/overview#prerequisites) ![Sign in google ads to create manager account](https://i.imgur.com/YbpJ3jP.png) ![Create manager account](https://i.imgur.com/GD9MHqI.png) ![API Center](https://i.imgur.com/luwaBJm.png) ![開發人員權杖](https://i.imgur.com/D64RVof.png) ## Google ads api 服務啟用 ### 登入GCP(Google Cloud Platform) ![啟用API服務](https://i.imgur.com/THdPUTc.png) ![啟用 Google ads api](https://i.imgur.com/yVXFuxo.png) ## 建立憑證 ![建立一組OAuth 用戶端憑證](https://i.imgur.com/J6DPAef.png) ![設定App類型與名稱](https://i.imgur.com/dUR7zNT.png) ![取得該憑證Client Id & Client Secret](https://i.imgur.com/MF9qIdq.png) ## 產生Refresh Token ### 以Java library為範例使用AuthenticateInDesktopApplication.java 1. [執行此Java class](https://github.com/googleads/google-ads-java/blob/master/google-ads-examples/src/main/java/com/google/ads/googleads/examples/authentication/AuthenticateInDesktopApplication.java) 2. 執行之前必須先填入Developer token + Client Id + Client Secret 3. 執行過程中會出現一段URL連結 copy到瀏覽器做授權的動作 ![Open the url on browser](https://i.imgur.com/eRlDdHG.png) 4. 允許該xxxx(App name)對該帳戶(使用申請Developer token的email帳戶)的Google廣告權限的存取 ![允許存取Google廣告](https://i.imgur.com/WZ2NOLr.png) 5. 複製該段授權碼於程式執行的console中貼上再繼續執行 ![複製授權碼](https://i.imgur.com/wCnw4Ba.png) 6. 取得Refresh token ![Get Refresh token](https://i.imgur.com/rEUnGmA.png) ## Make Api Call 1. 使用postman 取得access token (POST) https://www.googleapis.com/oauth2/v3/token?grant_type=refresh_token&client_id=請填上ClientId&client_secret=請填上ClientSecret&refresh_token=請填上RefreshToken ```JSON= { "access_token": "ya29.a0ARrdaM8CPk5e2cQ476a3CDII2_0grleZhyq_DY2i2_9WdZjv1rCBVjCzl9MTEYoTTvHUbVDx_9oQLvJRu4wkf_dYvv1dxCm3_G1KlPTSBmAReOc7gnJgsfGjVY5lLtduKLdiqnH74IV4L5kx7hKoCTyNz6ao", "expires_in": 3599, "scope": "https://www.googleapis.com/auth/adwords", "token_type": "Bearer" } ``` 2. Send request to api to get customer_client (POST) https://googleads.googleapis.com/v6/customers/目標帳戶CID/googleAds:search Request Header parameters ```header= Content-Type = application/json Authorization = Bearer 第一步驟的accesstoken developer-token = 請填上developer token login-customer-id = 目標帳戶的Manager account id ``` Request Body(raw) ```JSON= { "pageSize": 10000, "query": "SELECT customer_client.client_customer,customer_client.currency_code,customer_client.descriptive_name,customer_client.id,customer_client.level,customer_client.manager,customer_client.resource_name,customer_client.test_account,customer_client.time_zone FROM customer_client" } ``` ### **注意若Developer token屬於測試階段,則只能呼叫測試帳戶,一般的production帳戶無法呼叫** ![測試帳戶右上角會標示](https://i.imgur.com/lf6hmlf.png) ## 參考資源 * [Google Ads Api Overview](https://developers.google.com/google-ads/api/docs/first-call/overview#prerequisites) * [Java OAuth Desktop Application Flow](https://developers.google.com/google-ads/api/docs/client-libs/java/oauth-desktop)