Python 3.6 谷歌广告类型错误 : cannot use a string pattern on a bytes-like object

标签 python google-ads-api

我尝试与 Google Adwords API 建立联系使用 Python 3.6。我设法安装了这些库,得到了一个 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 in ...\Anaconda3\lib\site-packages\googleads-10.0.0-py3.6.egg\googleads\util.py",第 302 行,在过滤器中

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 .非常感谢您的帮助!

最佳答案

目前有两种解决方案:

一: 使用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

该方案修改了google提供的代码,为二进制字符串添加了utf编码,解决了我们的问题。

关于Python 3.6 谷歌广告类型错误 : cannot use a string pattern on a bytes-like object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49030383/

相关文章:

android - Google Play Install Referrer API 不提供 utmCampaign 信息

python - 矩阵乘法tensorflow与numpy的区别

python - Pandas :从行中的每个元素中减去行均值

python - 在 O(logn) 中添加、删除和插入元素

python - google adwords python api - 如何获取广告组出价

google-ads-api - 从 Google 再营销列表中删除用户

javascript - Google AdWords 转化跟踪的动态插入在 safari 中爆炸式增长

python 3.3.4 : python-daemon-3K ; How to use runner

python - 如何过滤Qtableview中的多列?

android - Adwords 转化跟踪不起作用