[英]How do I create a googleads yaml file for python googleads lib?
[英]I have a developer token and installed the googleads package. How do I start using the Google Ads API with Python?
我一直在尝试使用Python连接到Google Ads API。 我得到了开发人员令牌,并在Pycharm中安装了googleads软件包。 如何开始使用API? 在上个月的尝试中,尝试使用Google的教程无济于事。
我设法使用python进行了几次API调用,因此希望对您有所帮助。
这是我用来检索帐户下所有广告系列的功能:我使用AWQL版本,因为它更易于理解。
def getAllCampaigns(service):
"""
Retrieve all campaigns under a account.
No campaigns will be returned when this function is ran from
a MCC account (having a MCC ID in googleads.yaml).
"""
# Using AWQL to retrieve campaigns.
query = (adwords.ServiceQueryBuilder()
.Select('Id', 'Name', 'Status', 'StartDate', 'EndDate',
'BudgetId', 'BudgetStatus', 'BudgetName', 'Amount',
'BudgetReferenceCount', 'IsBudgetExplicitlyShared')
.Limit(0, pageSize)
.Build())
campaigns = []
# print('start loop')
for page in query.Pager(service):
# print(page['totalNumEntries'])
if page['entries']:
for campaign in page['entries']:
campaigns.append(campaign)
else:
print('no campaigns were found')
# print('end loop')
# print(campaigns)
return campaigns
此功能要求您传递“服务”。 要创建此类服务,您需要使用LoadFromStorage方法从googleads.yaml文件中检索信息。
使用以下行创建一个客户端,该客户端从googleads.yaml文件中检索信息,该文件默认为主目录,因此请确保当前位于该目录。
client = adwords.AdWordsClient.LoadFromStorage()
使用以下行来创建可以检索广告系列的服务。
service = client.GetService('CampaignService', version='v201809')
AdWords API文档的此页面包含所有可以使用的服务。 可能会有所帮助。
现在,您可以像上面这样调用上面创建的函数,它将打印帐户下的所有广告系列。
result = getAllCampaigns(service)
print(result)
请确保您的googleads.yaml文件包含(测试)客户帐户的ID,而不包含(测试)MCC帐户的ID,因为当包含(测试)MCC帐户的ID时,它不会打印任何广告系列。
我希望这个例子对您或任何可能来这里的人有所帮助。
与您同舟共济,但至少成功建立了连接(或者至少我认为是的)
该链接对我有用 : https : //developers.google.com/adwords/api/docs/guides/first-api-call
在步骤方面:
看来您已完成步骤1,(2)和3,猜测下一步将是4,并且您需要确保您具有通过google api控制台创建oauth2凭据的正确权限,请按照链接,然后您将获得刷新令牌。
不会撒谎,我本人坚持第6步...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.