Daniil
Daniil

Reputation: 15

How get list files from google drive with http request?

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

Answers (2)

Tanaike
Tanaike

Reputation: 201713

  • 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.

Upvotes: 1

Yosh
Yosh

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.

  1. Go to APIs Console and make your own project.
  2. Search for ‘Google Drive API’, select the entry, and click ‘Enable’.
  3. Select ‘Credentials’ from the left menu, click ‘Create Credentials’, select ‘OAuth client ID’.
  4. Now, the product name and consent screen need to be set -> click ‘Configure consent screen’ and follow the instructions. Once finished:
    1. Select ‘Application type’ to be Web application.
    2. Enter an appropriate name.
    3. Input http://localhost:8080 for ‘Authorized JavaScript origins’.
    4. Input http://localhost:8080/ for ‘Authorized redirect URIs’.
    5. Click ‘Save’.
  5. 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

Upvotes: 0

Related Questions