2013-04-25 182 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