2013-04-25 153 views
2

刚开始使用Adwords API时,出于某种原因,我似乎根本无法连接。Google 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')) 

回答

1

我不熟悉的AdWordsClient API,但你确定你的道路是正确的?

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

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

测试你可以在硬编码absoulte路径,以确保它不是一个路径问题

我也将确保'client_secrets.json存在,并且它是通过执行蟒蛇

用户可读
2

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

其次,看起来您正在使用OAuth2进行身份验证(我正在通过client_secrets.json文件猜测)。为此,您需要使用oauth2client库并在参数headersAdWordsClient的参数中提供一个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 
}) 
+0

多利安:这里的CLIENT_CUSTOMER_ID是一个变量。这个价值是否应该由客户提供,还是被视为保密价值? – 2013-07-11 18:09:06

+0

'client_customer_id'只是您想要访问的Google AdWords帐户的ID(“123-456-7890”)。 AFAIR可以忽略此参数,在这种情况下,您将访问连接到您的Google帐户的顶级Adwords帐户。 – dorian 2013-07-12 13:12:58