Reputation: 15
How I can get files list with access token? I read doc google drive, but i dont know how write requests for listing files. My Example:
rqf = requests.get('https://www.googleapis.com/drive/v3/files', headers=
{"Authorization": access_token})
Output
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
Upvotes: 1
Views: 2752
Reputation: 201713
requests.get()
.If my understanding for your question is correct, how about this modification?
headers
, please use {"Authorization": "Bearer " + accessToken}
.You can select the following 2 patterns for your situation.
import requests
access_token = "#####"
headers = {"Authorization": "Bearer " + access_token}
r = requests.get('https://www.googleapis.com/drive/v3/files', headers=headers)
print(r.text)
import requests
access_token = "#####"
r = requests.get('https://www.googleapis.com/drive/v3/files?access_token=' + access_token)
print(r.text)
If I misunderstand your question, I'm sorry.
Upvotes: 1
Reputation: 51
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.
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
Upvotes: 0