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

Can't save Credentials #491

Closed
depp05 opened this issue Apr 19, 2018 · 5 comments
Closed

Can't save Credentials #491

depp05 opened this issue Apr 19, 2018 · 5 comments
Assignees
Labels
🚨 This issue needs some love. triage me I really want to be triaged.

Comments

@depp05
Copy link

depp05 commented Apr 19, 2018

I am following the tutorial provided for the Youtube Data API

The following code :

# Sample Python code for user authorization
import os

from oauth2client.file import Storage

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow

CLIENT_SECRETS_FILE = "client_secret.json"

SCOPES = ['https://www.googleapis.com/auth/youtube']
API_SERVICE_NAME = 'youtube'
API_VERSION = 'v3'
CREDENTIALS = 'credentials.json'


def get_authenticated_service():
    flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
  
    storage = Storage(CREDENTIALS)
   
    credentials = storage.get()
    if not credentials or credentials.invalid:
        credentials = flow.run_local_server(host='localhost',
                                            port=8080,
                                            authorization_prompt_message='Please visit this URL: {url}',
                                            success_message='The auth flow is complete; you may close this window.',
                                            open_browser=True)

        storage.put(credentials)

    return build(API_SERVICE_NAME, API_VERSION, credentials=credentials)


def channels_list_by_username(service, **kwargs):
    results = service.channels().list(**kwargs).execute()
    print('This channel\'s ID is %s. Its title is %s, and it has %s views.' %
          (results['items'][0]['id'],
           results['items'][0]['snippet']['title'],
           results['items'][0]['statistics']['viewCount']))


if __name__ == '__main__':
    os.environ['OUATHLIB_INSECURE_TRANSPORT'] = '1'
    service = get_authenticated_service()
    channels_list_by_username(service, part='snippet,contentDetails,statistics',
                              forUsername='GoogleDevelopers')

Produces the following error :

Traceback (most recent call last):
  File "D:/Deep/Python/Youtube Playlist Trial/quickstart.py", line 61, in <module>
    service = get_authenticated_service()
  File "D:/Deep/Python/Youtube Playlist Trial/quickstart.py", line 46, in get_authenticated_service
    storage.put(credentials)
  File "D:\Deep\Python\Youtube Playlist Trial\youtube_trail\lib\site-packages\oauth2client\client.py", line 421, in put
    self.locked_put(credentials)
  File "D:\Deep\Python\Youtube Playlist Trial\youtube_trail\lib\site-packages\oauth2client\file.py", line 86, in locked_put
    f.write(credentials.to_json())
AttributeError: 'Credentials' object has no attribute 'to_json'

The storage of credentials is taken from this Guide

I think the Credential object provided by this library does not provided to_json() method, unlike the old library oauth2client.client.Credential, hence creating the error.

None of the guides or examples provided by this library or the above mentioned tutorial provides a way around this problem. It would be nice if you could provide a way to store the Credential object returned by this library or suggest an alternative way using this library to store.

P.S. : As the old library oauth2client is not supported, and this library is recommended. It would be nice if you could provide a way to store the Credential object returned from this library.

@theacodes
Copy link
Contributor

Can you give me the type of the credentials object here?

@theacodes
Copy link
Contributor

Oh, nevermind, I see what's going on.

You can't use oauth2client's storage with google-auth. google-auth credentials are relatively straight forward to persist, see: https://github.com/GoogleCloudPlatform/google-auth-library-python-oauthlib/blob/master/google_auth_oauthlib/tool/__main__.py#L80

@depp05
Copy link
Author

depp05 commented Apr 19, 2018

@jonparrott Thank you for such prompt reply.

If you don't mind me asking, where should one raise a request to update the samples and related docs?
I have been searching for an solution for a few days, and all the examples always went back to oauth2client Storage. It would be really useful for newbies like me, if this was either documented properly, or proper samples were at least provided.

@theacodes
Copy link
Contributor

For this specific case, you can use the star rating widget at the top of the page to both rate the page and send feedback.

@depp05
Copy link
Author

depp05 commented Apr 19, 2018

@jonparrott Thank you for your guidance.

@yoshi-automation yoshi-automation added 🚨 This issue needs some love. triage me I really want to be triaged. labels Apr 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚨 This issue needs some love. triage me I really want to be triaged.
Projects
None yet
Development

No branches or pull requests

3 participants