Google Adwords API身份验证问题

时间:2013-04-25 16:56:38

标签: python google-adwords

刚刚开始使用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'))

2 个答案:

答案 0 :(得分:2)

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

其次,您似乎正在使用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
})

答案 1 :(得分:1)

我不熟悉AdWordsClient api,但您确定自己的路径是正确的吗?

您当前的join会产生相对路径,您需要一个绝对路径吗?

>>> import os
>>> os.path.join('Users', 'this-user')
'Users/this-user'

对于测试,您可以对absoulte路径进行硬编码,以确保它不是路径问题

我还要确保'client_secrets.json存在,并且执行python的用户可以读取它