Reputation: 81
I'm trying to make an app where it displays the files and folders inside my google drive folder so I went to google apis and used the code I found on their page its here below
as I didn't expect it just let me log into my drive and then it returned me an error I'm providing a photo for it This is the error returned it says that
Message='client_secret' Source=D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py StackTrace: File "D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py", line 48, in main()
I managed to skip this error and renaming the json file and I solved this error only to find another error that says
Message=Authorized user info was not in the expected format, missing fields client_secret, client_id, refresh_token. Source=D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py StackTrace: File "D:\Study\Computer Science\G10\Paython 2nd semester\python consol app\python consol app\python_consol_app.py", line 48, in main()
so I'm wondering how should I solve such an issue also if my application can run into every single folder and subfolder
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
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
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)
with open('token.json', 'w') as token:
token.write(creds.to_json())
service = build('drive', 'v3', credentials=creds)
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()
Upvotes: 8
Views: 24687
Reputation: 1633
The solution for me was to log out of Google and then do the auth flow again.
Before logging out, I was seeing The authentication flow has completed. You may close this window.
After logging out I was re-prompted to authorize the APIs in 'scopes' which fixed the problem.
Upvotes: 0
Reputation: 710
Make sure that you renamed the downloaded credentials file as credentials.json
and also you enable the according google api (drive,..)
Upvotes: 3
Reputation: 129
first: Enable the API Before using Google APIs, you need to turn them on in a Google Cloud project. You can turn on one or more APIs in a single Google Cloud project. In the Google Cloud console, enable the Gmail API. from here
secound: Authorize credentials for a desktop application To authenticate as an end user and access user data in your app, you need to create one or more OAuth 2.0 Client IDs. A client ID is used to identify a single app to Google's OAuth servers. If your app runs on multiple platforms, you must create a separate client ID for each platform. In the Google Cloud console, go to Menu menu > APIs & Services > Credentials. Go to Credentials
Click Create Credentials > OAuth client ID. Click Application type > Desktop app. In the Name field, type a name for the credential. This name is only shown in the Google Cloud console. Click Create. The OAuth client created screen appears, showing your new Client ID and Client secret. Click OK. The newly created credential appears under OAuth 2.0 Client IDs.
last download JSON file and rename file to credentials.json, and move the file to your working directory.
my problem solved
Upvotes: 1
Reputation: 1461
First things first. It looks like you have some problem with your credentials. I would download the credentials file from the Quickstart again. Then, delete the generated token
file in your working directory.
If you want to go deeper, you can create a GCP project, and enable the Google API as well as generate the credentials, but this approach is not necessary if you don't want it.
Once you have Quickstart running, you can focus on your task. To display the files within your Drive, there is the Files: list method, which returns all the files and folders in the current user's My Drive. To perform specific searches, you can use different parameters, but the most important is the query
. You can read this guide for some usage examples and further explanation.
Upvotes: 4