Forums

googleapiclientTypeError: argument should be integer or bytes-like object, not 'str'

My code opens retrieves a spreadsheet data. It works on my computer but it wont work on pythonanywhere. I have no idea what could be wrong... Can anyone help me?

from google.oauth2 import service_account
from googleapiclient import discovery

SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
SERVICE_ACCOUNT_FILE = 'zapsfpo-001d5b271730.json'
SAMPLE_SPREADSHEET_ID = '154v5VdcZJrNRbz6cEWd7J5K0AnUgjAnU8GdHOaABUt8'
SAMPLE_RANGE_NAME = 'PROCESSOS!A3:L'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = discovery.build('sheets','v4',credentials=credentials)
result = service.spreadsheets().values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE_NAME).execute()
print(result)

Here is the error:

Traceback (most recent call last):
  File "/home/cunha00/mysite/test.py", line 13, in <module>
    service = discovery.build('sheets','v4',credentials=credentials)
  File "/home/cunha00/.local/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/cunha00/.local/lib/python3.7/site-packages/googleapiclient/discovery.py", line 223, in build
    requested_url, discovery_http, cache_discovery, cache, developerKey)
  File "/home/cunha00/.local/lib/python3.7/site-packages/googleapiclient/discovery.py", line 273, in _retrieve_discovery_doc
    resp, content = http.request(actual_url)
  File "/home/cunha00/.local/lib/python3.7/site-packages/httplib2/__init__.py", line 1991, in request
    cachekey,
  File "/home/cunha00/.local/lib/python3.7/site-packages/httplib2/__init__.py", line 1651, in _request
    conn, request_uri, method, body, headers
  File "/home/cunha00/.local/lib/python3.7/site-packages/httplib2/__init__.py", line 1557, in _conn_request
    conn.connect()
Traceback (most recent call last):
  File "/home/cunha00/mysite/test.py", line 13, in <module>
    service = discovery.build('sheets','v4',credentials=credentials)
  File "/home/cunha00/.local/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/cunha00/.local/lib/python3.7/site-packages/googleapiclient/discovery.py", line 223, in build
    requested_url, discovery_http, cache_discovery, cache, developerKey)
  File "/home/cunha00/.local/lib/python3.7/site-packages/googleapiclient/discovery.py", line 273, in _retrieve_discovery_doc
    resp, content = http.request(actual_url)
  File "/home/cunha00/.local/lib/python3.7/site-packages/httplib2/__init__.py", line 1991, in request
    cachekey,
  File "/home/cunha00/.local/lib/python3.7/site-packages/httplib2/__init__.py", line 1651, in _request
    conn, request_uri, method, body, headers
  File "/home/cunha00/.local/lib/python3.7/site-packages/httplib2/__init__.py", line 1557, in _conn_request
    conn.connect()
Traceback (most recent call last):
  File "/home/cunha00/mysite/test.py", line 11, in <module>
    service = discovery.build('sheets','v4',credentials=credentials)
  File "/home/cunha00/.local/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/cunha00/.local/lib/python3.7/site-packages/googleapiclient/discovery.py", line 223, in build
    requested_url, discovery_http, cache_discovery, cache, developerKey)
  File "/home/cunha00/.local/lib/python3.7/site-packages/googleapiclient/discovery.py", line 273, in _retrieve_discovery_doc
    resp, content = http.request(actual_url)
  File "/home/cunha00/.local/lib/python3.7/site-packages/httplib2/__init__.py", line 1991, in request
    cachekey,
  File "/home/cunha00/.local/lib/python3.7/site-packages/httplib2/__init__.py", line 1651, in _request
  File "/home/cunha00/.local/lib/python3.7/site-packages/googleapiclient/discovery.py", line 273, in _retrieve_discovery_doc
    conn, request_uri, method, body, headers
  File "/home/cunha00/.local/lib/python3.7/site-packages/httplib2/__init__.py", line 1557, in _conn_request
    conn.connect()
  File "/home/cunha00/.local/lib/python3.7/site-packages/httplib2/__init__.py", line 1324, in connect
    sock.connect((self.host, self.port))
  File "/usr/lib/python3.7/site-packages/socks.py", line 406, in connect
    self.__negotiatehttp(destpair[0],destpair[1])
  File "/usr/lib/python3.7/site-packages/socks.py", line 357, in __negotiatehttp
    while resp.find("\r\n\r\n")==-1:
TypeError: argument should be integer or bytes-like object, not 'str'

You may be getting that because you have not configured the google api client to use the proxy. Have a look at https://www.pythonanywhere.com/forums/topic/13179/ for how to do that.

have the same issue with google services. configuring proxy doesn't help. proxy is read from environment correctly.

According to this issue installing a more recent PySocks may help.

thanks. works for me now!