Python 3.6 Googleads TypeError:不能在类字节对象上使用字符串模式

问题描述 投票:2回答:1

我尝试使用Python 3.6与Google Adwords API建立连接。我设法安装了库,得到了developer tokenclient_customer_iduser_agentclient_idclient_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提供的示例。非常感谢帮助!

python google-adwords
1个回答
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编码,这解决了我们的问题。

© www.soinside.com 2019 - 2024. All rights reserved.