2

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

2 回答 2

2

好像有两个问题。首先,尝试删除最后一个路径元素,据我记得,该path参数需要一个包含身份验证泡菜、日志等的目录。这种方法要求您已经有一个有效的auth_token.pkl.

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

以下直接来自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
})
于 2013-05-07T09:59:02.340 回答
1

我对AdWordsClientapi不熟悉,但你确定你的路径是正确的吗?

你的电流join产生一个相对路径,你需要一个绝对路径吗?

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

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

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

于 2013-04-25T17:07:42.813 回答