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

AttributeError in credentials.py #405

Closed
ryoichitaniguchi opened this issue Dec 10, 2019 · 10 comments
Closed

AttributeError in credentials.py #405

ryoichitaniguchi opened this issue Dec 10, 2019 · 10 comments
Assignees
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. 🚨 This issue needs some love. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@ryoichitaniguchi
Copy link

Hello,

after updating google-auth package from v1.7.2 to v1.8.0 error occurred from credentials.py

stack-trace:

    range=RANGE).execute()
  File "/home/vagrant/py36/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/vagrant/py36/lib/python3.6/site-packages/googleapiclient/http.py", line 851, in execute
    method=str(self.method), body=self.body, headers=self.headers)
  File "/home/vagrant/py36/lib/python3.6/site-packages/googleapiclient/http.py", line 165, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "/home/vagrant/py36/lib/python3.6/site-packages/google_auth_httplib2.py", line 187, in request
    self._request, method, uri, request_headers)
  File "/home/vagrant/py36/lib/python3.6/site-packages/google/auth/credentials.py", line 125, in before_request
    self.apply(headers)
  File "/home/vagrant/py36/lib/python3.6/site-packages/google/oauth2/credentials.py", line 185, in apply
    if self.quota_project_id is not None:
  File "/home/vagrant/py36/lib/python3.6/site-packages/google/oauth2/credentials.py", line 134, in quota_project_id
    return self._quota_project_id
AttributeError: 'Credentials' object has no attribute '_quota_project_id'

Trigger is googleapiclient.http.execute():

from googleapiclient.discovery import build
build('sheets', 'v4', credentials=creds).spreadsheets().values().get(spreadsheetId=SPREADSHEET_ID,range=RANGE).execute()

Downgrading google-auth to previous version (or avoid to refer this attribute) , error dissapeared.

Some compatibility issue behind this? Appreciate if someone let me know..

requirements condition

google-api-python-client
google-auth-httplib2
google-auth-oauthlib
google-auth

installed version from pip-list

google-api-python-client (1.7.11)
google-auth (1.8.0)     # or 1.8.1
google-auth-httplib2 (0.0.3)
google-auth-oauthlib (0.4.1)
@maxiller
Copy link

@ryoichitaniguchi thanks for the suggestion - rolling back to google-auth==1.7.2 workarounded for me

@busunkim96
Copy link
Contributor

@maxiller @ryoichitaniguchi Were you using a pickled credential file?

Please do continue to use the downgraded version 1.7.2 - we will release a patch version soon with a fix. Thank you for your patience

@busunkim96
Copy link
Contributor

busunkim96 commented Dec 10, 2019

The Docs API quickstart is representative of the sample style for APIs requiring user credentials. It pickles the credentials after the first run and reloads them when available.

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/documents.readonly']

# The ID of a sample document.
DOCUMENT_ID = '195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE'

def main():
    """Shows basic usage of the Docs API.
    Prints the title of a sample document.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('docs', 'v1', credentials=creds)

    # Retrieve the documents contents from the Docs service.
    document = service.documents().get(documentId=DOCUMENT_ID).execute()

    print('The title of the document is: {}'.format(document.get('title')))


if __name__ == '__main__':
    main()

@gayhub-dev
Copy link

damn! Thought something happen to my code!

@GustavoKatel
Copy link

GustavoKatel commented Dec 10, 2019

I created new credentials based on the old one:

new_cred = Credentials(token=cred.token, refresh_token=cred.refresh_token, id_token=cred.id_token, token_uri=cred.token_uri, client_id=cred.client_id, client_secret=cred.client_secret, scopes=cred.scopes)

Then I did pickle again to file

@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Dec 10, 2019
@busunkim96 busunkim96 added priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. and removed triage me I really want to be triaged. labels Dec 11, 2019
@busunkim96
Copy link
Contributor

Hi -

If you're running into this, please do either (1) or (2).

(1) Delete your pickled credentials file. The file will be named token.pickle if you followed a Google quickstart. Re-run the script (code that runs the authorization flow).
(2) Pin to a previous version google-auth==1.7.2

@maxiller
Copy link

Hi -

If you're running into this, please do either (1) or (2).

(1) Delete your pickled credentials file. The file will be named token.pickle if you followed a Google quickstart. Re-run the script (code that runs the authorization flow).
(2) Pin to a previous version google-auth==1.7.2

(1) did the trick. Thanks!

@ryoichitaniguchi
Copy link
Author

worked with google-auth==1.8.2 in my environment as well, thanks @busunkim96

@busunkim96
Copy link
Contributor

Closing as the latest published library should work correctly with older pickled credentials files.

@gnsawant
Copy link

gnsawant commented Feb 1, 2023

I am unable to install gspread with python 3.9.12 version, it throws error "module 'google.auth.credentials' has no attribute 'CredentialsWithTokenUri' "

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. 🚨 This issue needs some love. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

7 participants