2013-07-22 85 views
0

我無法在Python客戶端中使用下載報告功能。我使用adwords-15.9.0和v201306。它總是失敗:「TypeError:無法連接Python中的'str'和'NoneType'對象Google Adwords API客戶端

$ ./classifications.py 
Traceback (most recent call last): 
    File "./classifications.py", line 48, in <module> 
    download_report(client, client_id) 
    File "./classifications.py", line 32, in download_report 
    file_path = report_downloader.DownloadReportWithAwql(report_query, 'CSV', file_path=path) 
    File "/Users/mike/.virtualenvs/xxx/lib/python2.7/site-packages/adspygoogle/adwords/ReportDownloader.py", line 127, in DownloadReportWithAwql 
    fileobj) or file_path 
    File "/Users/mike/.virtualenvs/xxx/lib/python2.7/site-packages/adspygoogle/adwords/ReportDownloader.py", line 169, in __DownloadAdHocReportWithAwql 
    return self.__DownloadReport(payload, return_micros, fileobj) 
    File "/Users/mike/.virtualenvs/xxx/lib/python2.7/site-packages/adspygoogle/adwords/ReportDownloader.py", line 184, in __DownloadReport 
    headers = self.__GenerateHeaders(return_micros) 
    File "/Users/mike/.virtualenvs/xxx/lib/python2.7/site-packages/adspygoogle/adwords/ReportDownloader.py", line 282, in __GenerateHeaders 
    self._headers['oauth2credentials'].apply(headers) 
    File "/Users/mike/.virtualenvs/xxx/lib/python2.7/site-packages/oauth2client/client.py", line 533, in apply 
    headers['Authorization'] = 'Bearer ' + self.access_token 
TypeError: cannot concatenate 'str' and 'NoneType' objects 

示例腳本get_report_fields.pyget_campaign_stats.py做工精細,但download_criteria_report.pydownload_criteria_report_with_awql.py失敗,同樣的錯誤。

任何想法?

我的代碼:由OAuth2Credentials對象的屬性access_tokenNone所示

#!/usr/bin/env python 

import csv 
import os 
import MySQLdb as mdb 
from adspygoogle.adwords.AdWordsClient import AdWordsClient 


MATCH_TYPES = { 
    'b': 'Broad', 
    'e': 'Exact', 
    'p': 'Phrase', 
} 

DEVICE_TYPES = { 
    'c': 'Desktop', 
    'm': 'Mobile', 
    't': 'Tablet', 
} 

REPORT_TYPE = 'CREATIVE_CONVERSION_REPORT' 


def download_report(client, client_id): 
    # Initialize appropriate service. 
    report_downloader = client.GetReportDownloader(version='v201306') 

    # Create report query. 
    report_query = ('SELECT AdGroupId', 'CampaignId', 'CreativeId FROM CREATIVE_CONVERSION_REPORT DURING LAST_7_DAYS') 

    path = '/tmp/report_%d.csv' % client_id 
    file_path = report_downloader.DownloadReportWithAwql(report_query, 'CSV', file_path=path) 

    print 'Report was downloaded to \'%s\'.' % file_path 

if __name__ == '__main__': 
    client = AdWordsClient() 

    conn = mdb.connect('xxx.us-east-1.rds.amazonaws.com', 'xxx', 'xxx', 'xxx'); 
    with conn: 
     cur = conn.cursor(mdb.cursors.DictCursor) 
     cur.execute("SELECT * FROM xxx.adwords_accounts") 

     rows = cur.fetchall() 
     for row in rows: 
      client_id = row['id'] 
      client.SetClientCustomerId(client_id) 
      download_report(client, client_id) 

回答

0

什麼是錯的與您的身份驗證。

如果你沒有則已,一起來看看在use_oath2.py例子來看看通過的OAuth2認證是如何處理的。您還需要創建一個Google API Console應用程序來獲取客戶端ID和密碼。

相關問題