Google Ads API 获取
最近由于公司的一些项目需要对google ads 平台进行优化,需要将公司后台数据管道导入部分客户数据到google ads进行精准客户定位,所以需要用到一些api, 在对google ads 进行开发的前提我想第一个步骤就是需要准备密钥,本文主要对前期密钥准备做一些介绍希望能帮你快速架设开发环境把精力放在实际项目开发当中!
1.需要获取developer token
大家可以在Tools and setting -> API Center 填写一个表单申请一个API 这个步骤完成后请保留api key 后面开发当中需要用到.
2.https://console.cloud.google.com/
登陆到谷歌云当中,选择你的project,如果没有可以创建一个新的project
一定要enable 你的google ads API
3.进入 APIs & Services -> OAuth consent screen 按照你的正常信息填写表单创OAuth ,这里的重点在于第二步 scope 请一定要选好google ads
4.当上步骤完成后返回Credentials, 然后点击Create OAuth client ID, 创建一个Client ID 和 Client secret 就可以了。 关于这个application type建议大家选择web 就好了,这个token 可以用于任何python 或java 脚本。
5.最终需要获取这个refresh token 这个很重要
google.auth.exceptions.RefreshError: ('unauthorized_client: Unauthorized', {'error': 'unauthorized_client', 'error_description': 'Unauthorized'})
之前我的refresh token 获取错误了,导致以上错误!主要原因就是refresh token 没正确获取, 这里建议可以通过一下代码获取,替换下这个client_id, client_secrete即可,
记得这个redirect_uris 必须在第四步配好,不然会报错
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
flow = InstalledAppFlow.from_client_config(
{'installed': {
'client_id': '',
'client_secret': '',
'redirect_uris': ["http://localhost:8080"],
'auth_uri': 'https://accounts.google.com/o/oauth2/auth',
'token_uri': 'https://accounts.google.com/o/oauth2/token'
}},
scopes=['https://www.googleapis.com/auth/adwords']
)
creds = flow.run_local_server(port=8080)
with open('ref-token.json', 'w') as token:
token.write(creds.to_json())
通过以上几个步骤你已经获取了 Developer token/ Client_id, Client_secrete/Refresh token 现在可以配置你的程序里面开发你的api开发了。希望这个文章对你有所帮助。 谢谢