Reputation: 49
I'm having an error with the Google Drive API. I'm getting a key error for "client_secret". Most of this code is straight from Google documentation. Has anyone else had this issue and/or knows a solution?
Code:
from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
def main():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
# The file token.json 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.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# 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.json', 'w') as token:
token.write(creds.to_json())
service = build('drive', 'v3', credentials=creds)
# Call the Drive v3 API
results = service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print(u'{0} ({1})'.format(item['name'], item['id']))
if __name__ == '__main__':
main()
Error:
Traceback (most recent call last):
File "C:/Users/holym/PycharmProjects/HackPhoenix2021/quickstart.py", line 48, in <module>
main()
File "C:/Users/holym/PycharmProjects/HackPhoenix2021/quickstart.py", line 28, in main
creds = flow.run_local_server(port=0)
File "C:\Users\holym\AppData\Local\Programs\Python\Python38\lib\site-packages\google_auth_oauthlib\flow.py", line 474, in run_local_server
self.fetch_token(authorization_response=authorization_response)
File "C:\Users\holym\AppData\Local\Programs\Python\Python38\lib\site-packages\google_auth_oauthlib\flow.py", line 286, in fetch_token
kwargs.setdefault("client_secret", self.client_config["client_secret"])
KeyError: 'client_secret'
Upvotes: 2
Views: 3618
Reputation: 31
This is because the credentials.json does not have the "client_secret" value. What you need to do is go to the console, edit the OAuth client, "Reset Secret" and then redownload the json file again.
Upvotes: 2
Reputation: 721
The issue is likely the contents of your credentials.json file.
See also Setting up OAuth 2.0.
The credentials.json file should look like:
{
"installed": {
"client_id": "1234-abcd.apps.googleusercontent.com",
"project_id": "myproject",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "generatedsecret",
"redirect_uris": [
"urn:ietf:wg:oauth:2.0:oob",
"http://localhost"
]
}
}
Upvotes: 2