###### 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)




## Google ads api 服務啟用
### 登入GCP(Google Cloud Platform)


## 建立憑證



## 產生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到瀏覽器做授權的動作

4. 允許該xxxx(App name)對該帳戶(使用申請Developer token的email帳戶)的Google廣告權限的存取

5. 複製該段授權碼於程式執行的console中貼上再繼續執行

6. 取得Refresh token

## 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帳戶無法呼叫**

## 參考資源
* [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)