gpt4 book ai didi

python - Google Adwords API 身份验证问题

转载 作者:太空宇宙 更新时间:2023-11-04 01:28:06 25 4
gpt4 key购买 nike

刚开始使用 Adwords API,出于某种原因我似乎根本无法连接。

下面的代码,直接来自教程会引发错误:

Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
client = AdWordsClient(path=os.path.join('Users', 'ravinthambapillai', 'Google Drive', 'client_secrets.json'))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/adspygoogle/adwords/AdWordsClient.py", line 151, in __init__
self._headers = self.__LoadAuthCredentials()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/adspygoogle/adwords/AdWordsClient.py", line 223, in __LoadAuthCredentials
return super(AdWordsClient, self)._LoadAuthCredentials()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/adspygoogle/common/Client.py", line 94, in _LoadAuthCredentials
raise ValidationError(msg)
**ValidationError: Authentication data is missing.**

from adspygoogle.adwords.AdWordsClient import AdWordsClient
from adspygoogle.common import Utils
client = AdWordsClient(path=os.path.join('Users', 'this-user', 'this-folder', 'client_secrets.json'))

最佳答案

看起来有两个问题。首先,尝试删除最后一个路径元素,据我所知,path 参数需要一个包含身份验证 pickle、日志等的目录。这种方法要求您已经拥有有效的 auth_token .pkl.

其次,您似乎正在使用 OAuth2 进行身份验证(我根据 client_secrets.json 文件猜测)。为此,您需要使用 oauth2client 库并在 headers 参数中为 AdWordsClient 提供一个 oauth2credentials 实例。

以下内容直接来自客户端分发中的文件 examples/adspygoogle/adwords/v201302/misc/use_oauth2.py,应该让您了解它是如何工作的:

# We're using the oauth2client library:
# http://code.google.com/p/google-api-python-client/downloads/list
flow = OAuth2WebServerFlow(
client_id=oauth2_client_id,
client_secret=oauth2_client_secret,
# Scope is the server address with '/api/adwords' appended.
scope='https://adwords.google.com/api/adwords',
user_agent='oauth2 code example')

# Get the authorization URL to direct the user to.
authorize_url = flow.step1_get_authorize_url()

print ('Log in to your AdWords account and open the following URL: \n%s\n' %
authorize_url)
print 'After approving the token enter the verification code (if specified).'
code = raw_input('Code: ').strip()

credential = None
try:
credential = flow.step2_exchange(code)
except FlowExchangeError, e:
sys.exit('Authentication has failed: %s' % e)

# Create the AdWordsUser and set the OAuth2 credentials.
client = AdWordsClient(headers={
'developerToken': '%s++USD' % email,
'clientCustomerId': client_customer_id,
'userAgent': 'OAuth2 Example',
'oauth2credentials': credential
})

关于python - Google Adwords API 身份验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16220574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com