question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't save Credentials

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
depp05commented, 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.

0reactions
depp05commented, Apr 19, 2018

@jonparrott Thank you for your guidance.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix: Saved RDP Credentials Didn't Work on Windows
Click “Show Options” in the Remote Desktop Connection window and make sure that “Always ask for credentials” option is not checked; · If...
Read more >
Remote Desktop Won't Save Username and Password
Method 2 – Set One System to Save Credentials · Select the “Start” button, then type “credential“. · Open “Credential Manager“. · Select...
Read more >
Fix Windows Remote Desktop does not save credentials
If Windows Remote Desktop is not saving Credentials, then check & change these settings. This will help save the password in RDP.
Read more >
FIX: Windows does not save Remote Desktop Credentials ...
How to FIX: Windows Won't Save Remote Desktop Username and Password and always prompts for credentials. · 1. Open Group Policy on the...
Read more >
Easily Fixed: Remote Desktop Not Saving Credentials
Method 3. Allow delegating credentials and add servers. Enabling the policy settings to specify the servers so that RDP won't appear not saving...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found