Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with authorizing credentials #718

Closed
bradboldenow opened this issue Dec 25, 2019 · 9 comments
Closed

issue with authorizing credentials #718

bradboldenow opened this issue Dec 25, 2019 · 9 comments

Comments

@bradboldenow
Copy link

bradboldenow commented Dec 25, 2019

I am running the basic authentication process (after generating the OAuth credentials outlined in the documentation: https://gspread.readthedocs.io/en/latest/oauth2.html

Here is my code for reference:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name('my_creds-06044a753234.json', scope)

gc = gspread.authorize(credentials)

wks = gc.open("My Sheet").sheet1

I am receiving a type error, saying the argument for the authorize function in my code is a string. Has anyone seen this issue? I was not able to find any other issues where this was replicated. I am using python 3.7 and am running this code on python anywhere: https://www.pythonanywhere.com/ Any ideas would be greatly appreciated.

Full Error:

Traceback (most recent call last):
  File "/home/data/DTR_GoogleSheets/test.py", line 9, in <module>
    gc = gspread.authorize(credentials)
  File "/home/data/.local/lib/python3.7/site-packages/gspread/__init__.py", line 38, in authorize
    client.login()
  File "/home/data/.local/lib/python3.7/site-packages/gspread/client.py", line 51, in login
    self.auth.refresh(http)
  File "/home/data/.local/lib/python3.7/site-packages/oauth2client/client.py", line 545, in refresh
    self._refresh(http)
  File "/home/data/.local/lib/python3.7/site-packages/oauth2client/client.py", line 749, in _refresh
    self._do_refresh_request(http)
  File "/home/data/.local/lib/python3.7/site-packages/oauth2client/client.py", line 780, in _do_refresh_request
    body=body, headers=headers)
  File "/home/data/.local/lib/python3.7/site-packages/oauth2client/transport.py", line 282, in request
    connection_type=connection_type)
  File "/usr/lib/python3.7/site-packages/httplib2/__init__.py", line 1957, in request
    cachekey,
  File "/usr/lib/python3.7/site-packages/httplib2/__init__.py", line 1622, in _request
    conn, request_uri, method, body, headers
  File "/usr/lib/python3.7/site-packages/httplib2/__init__.py", line 1528, in _conn_request
    conn.connect()
  File "/usr/lib/python3.7/site-packages/httplib2/__init__.py", line 1309, 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'
@jaidencreep
Copy link

Try opening your sheet by key using sh = gc.open_by_key(INSERT YOUR KEY HERE) then `wks = sh.worksheet(INSERT WORKSHEET NAME)

@bradboldenow
Copy link
Author

Hi @CubeBlazer, thanks for the response. The issue doesn't seem to be with opening the sheet. It seems to be in authorizing using my credentials ( i.e. gc = gspread.authorize(credentials) is the failure point based on the error.)

@msvanberg
Copy link

FWIW having the same problem, also on PythonAnywhere. It runs on my local machine though...

@danspanner
Copy link

danspanner commented Feb 17, 2020

I am also having auth issues.

`#!/usr/bin/env python3

import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds','https://googleapis.com/auth/drive']
#scope = ['https://googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('service.json', scope)
gc = gspread.authorize(credentials)
#wks = gc.open("Test API Sheet").sheet1
sht1 = gc.open_by_key("1i9n3GRMa33H8...9yFiBxaXU")
`

After following the Read the Docs page here
https://gspread.readthedocs.io/en/latest/oauth2.html

File "./test.py", line 12, in <module> gc = gspread.authorize(credentials) File "/home/dan/Documents/code/gsheets_poc/lib/python3.7/site-packages/gspread/__init__.py", line 38, in authorize client.login() File "/home/dan/Documents/code/gsheets_poc/lib/python3.7/site-packages/gspread/client.py", line 51, in login self.auth.refresh(http) File "/home/dan/Documents/code/gsheets_poc/lib/python3.7/site-packages/oauth2client/client.py", line 545, in refresh self._refresh(http) File "/home/dan/Documents/code/gsheets_poc/lib/python3.7/site-packages/oauth2client/client.py", line 749, in _refresh self._do_refresh_request(http) File "/home/dan/Documents/code/gsheets_poc/lib/python3.7/site-packages/oauth2client/client.py", line 819, in _do_refresh_request raise HttpAccessTokenRefreshError(error_msg, status=resp.status) oauth2client.client.HttpAccessTokenRefreshError: invalid_scope: Invalid oauth scope or ID token audience provided.

I noticed in the documentation that it refers to a "Service Key" as opposed to a Service Account- is it possible the documentation is out of date and there are further steps to the auth process?

Edit: I have left other endpoints I tried to auth against in the code. This is running from my local machine, Python 3.7 on Debian 10.

pip3 list
Package Version


certifi 2019.11.28
chardet 3.0.4
gspread 3.2.0
httplib2 0.17.0
idna 2.8
oauth2client 4.1.3
pip 18.1
pkg-resources 0.0.0
pyasn1 0.4.8
pyasn1-modules 0.2.8
requests 2.22.0
rsa 4.0
setuptools 40.8.0
six 1.14.0
urllib3 1.25.8

@msvanberg
Copy link

msvanberg commented Feb 20, 2020

My issue was fixed by running pip3.7 install --user --force-reinstall PySocks in a Bash console on the platform.

@alihassan10020161
Copy link

check this link. it resolve my same problem

https://www.youtube.com/watch?v=cnPlKLEGR7E

@kyluca
Copy link

kyluca commented May 12, 2020

Hey all, I was having similar OAuth issues when trying to update some old code (using gspread==0.6.2) to use the latest versions (gspread>=3.0.0) and I believe it might be the same solution required here.
The scopes OP is using were the same as mine, https://spreadsheets.google.com/feeds, which worked for old versions of the Google Sheets API. But now we should be using v4 of the Google Sheets API with the correct scope https://www.googleapis.com/auth/spreadsheets.

As per the Google Sheets API docs: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get#authorization-scopes

Requires one of the following OAuth scopes:

Hope that helps someone else too.

TL;DR: Change https://spreadsheets.google.com/feeds to https://www.googleapis.com/auth/spreadsheets

@marius-ne
Copy link

My issue was fixed by running pip3.7 install --user --force-reinstall PySocks in a Bash console on the platform.

This worked for me as well! Had the same issue as OP while running on PythonAnywhere.

@lavigne958
Copy link
Collaborator

Issues seems resolved. we now use the new scopes for the API V4 and it's all provided by default by gspread suing gspread.oauth().

closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants