Dendrobates
Dendrobates

Reputation: 3534

Python 3.6 Googleads TypeError: cannot use a string pattern on a bytes-like object

I try to make connection with the Google Adwords API using Python 3.6. I managed to install the libraries, got a developer token, client_customer_id, user_agent, client_id, client_secret and requested succesfully a refresh_token.

My googleads.yaml file looks like this:

adwords:
  developer_token: hta...
  client_customer_id: 235-...-....
  user_agent: mycompany
  client_id: 25785...apps.googleusercontent.com
  client_secret: J9Da...
  refresh_token: 1/ckhGH6...

When running the first python script get_campaigns.py, I get the very generic response 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", line 302, in filter

Other functions like traffic_estimator_service.get(selector) produce the same error. Furthermore, when starting the Python script get_campaigns.py, I get the following warning, which might explains something:

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}

I tried many things, but still can't find what causes my error. My settings seem to be right, and I use the examples as provided here. Help is highly appreciated!

Upvotes: 2

Views: 751

Answers (1)

Karioki
Karioki

Reputation: 664

There are two solutions for now:

One: Use Python2.7, solved this error for me.

Two: For 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

This solution changes code provided by google and add utf encoding for the binary string, which solves our problem.

Upvotes: 1

Related Questions