我尝试使用Python 3.6与Google Adwords API建立连接。我设法安装了这些库,获得了developer token
,client_customer_id
,user_agent
,client_id
,client_secret
,并成功请求refresh_token
。
我的googleads.yaml文件如下所示:
adwords:
developer_token: hta...
client_customer_id: 235-...-....
user_agent: mycompany
client_id: 25785...apps.googleusercontent.com
client_secret: J9Da...
refresh_token: 1/ckhGH6...
运行第一个python脚本get_campaigns.py
时,我在TypeError: cannot use a string pattern on a bytes-like object
...\Anaconda3\lib\site-packages\googleads-10.0.0-py3.6.egg\googleads\util.py", line 302, in filter
traffic_estimator_service.get(selector)
等其他功能会产生相同的错误。此外,在启动Python脚本get_campaigns.py
时,我收到以下警告,这可能会解释一些事情:
WARNING:googleads.common:Your default encoding, cp1252, is not UTF-8. Please run this script with UTF-8 encoding to avoid errors.
INFO:oauth2client.client:Refreshing access_token
INFO:googleads.common:Request summary - {'methodName': get, 'clientCustomerId': xxx-xxx-xxxx}
我尝试了很多东西,但仍然无法找到导致我错误的原因。我的设置似乎是正确的,我使用here提供的示例。非常感谢帮助!
答案 0 :(得分:1)
目前有两种解决方案:
一: 使用Python2.7,为我解决了这个错误。
二: 对于python 3
def method_waraper(self, record):
def filter(self, record):
if record.args:
arg = record.args[0]
if isinstance(arg, suds.transport.Request):
new_arg = suds.transport.Request(arg.url)
sanitized_headers = arg.headers.copy()
if self._AUTHORIZATION_HEADER in sanitized_headers:
sanitized_headers[self._AUTHORIZATION_HEADER] = self._REDACTED
new_arg.headers = sanitized_headers
msg = arg.message
if sys.version_info.major < 3:
msg = msg.decode('utf-8')
new_arg.message = self._DEVELOPER_TOKEN_SUB.sub(
self._REDACTED, str(msg, encoding='utf-8'))
record.args = (new_arg,)
return filter(self, record)
googleads.util._SudsTransportFilter.filter = method_waraper
此解决方案更改了谷歌提供的代码,并为二进制字符串添加了utf编码,这解决了我们的问题。