2013-07-22 60 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和密码。

相关问题