Can't save Credentials
See original GitHub issueI 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:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
@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.@jonparrott Thank you for your guidance.