How Get List Files From Google Drive With Http Request?
Solution 1:
- You want to retrieve the file list using Drive API v3 by
requests.get()
.
If my understanding for your question is correct, how about this modification?
Modification points:
- When it uses
headers
, please use{"Authorization": "Bearer " + accessToken}
. - You can also use the access token as the query parameter.
You can select the following 2 patterns for your situation.
Pattern 1: Using headers
import requests
access_token = "#####"
headers = {"Authorization": "Bearer " + access_token}
r = requests.get('https://www.googleapis.com/drive/v3/files', headers=headers)
print(r.text)
Pattern 2: Using query parameter
import requests
access_token = "#####"
r = requests.get('https://www.googleapis.com/drive/v3/files?access_token=' + access_token)
print(r.text)
Note:
- This modified script supposes as follows.
- Drive API has already been enabled at API console.
- The scope for retrieving the file list is included in the scope of access token.
References:
If I misunderstand your question, I'm sorry.
Solution 2:
Use PyDrive
module to deal with google drive, I don't know if like to work with it. But if you want follow the following instructions, further information read PyDrive’s documentation.
- Go to APIs Console and make your own project.
- Search for ‘Google Drive API’, select the entry, and click ‘Enable’.
- Select ‘Credentials’ from the left menu, click ‘Create Credentials’, select ‘OAuth client ID’.
- Now, the product name and consent screen need to be set -> click ‘Configure consent screen’ and follow the instructions. Once finished:
- Select ‘Application type’ to be Web application.
- Enter an appropriate name.
- Input http://localhost:8080 for ‘Authorized JavaScript origins’.
- Input http://localhost:8080/ for ‘Authorized redirect URIs’.
- Click ‘Save’.
- Click ‘Download JSON’ on the right side of Client ID to download
client_secret_<really long ID>.json
.
The downloaded file has all authentication information of your application. Rename the file to client_secrets.json
and place it in your working directory.
Create quickstart.py
file and copy and paste the following code.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
# Make auth
gauth = GoogleAuth()
gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication.
Run this code with python quickstart.py and you will see a web browser asking you for authentication. Click Accept and you are done with authentication. For more details, take a look at documentation: OAuth made easy
Getting list of files
PyDrive
handles paginations and parses response as list of GoogleDriveFile. Let’s get title and id of all the files in the root folder of Google Drive. Again, add the following code to quickstart.py
and execute it.
drive = GoogleDrive(gauth)
# Auto-iterate through all files that matches this query
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
You will see title and id of all the files and folders in root folder of your Google Drive. For more details, take a look at documentation: File listing made easy
Post a Comment for "How Get List Files From Google Drive With Http Request?"