import urllib.parse
import urllib.request
import json
def get_googledrive_item(access_token: str, item_id: str):
# Get metadata for a specific file/folder
url = f"https://www.googleapis.com/drive/v3/files/{item_id}"
headers = {'Authorization': 'Bearer ' + access_token}
request = urllib.request.Request(url, method="GET", headers=headers)
with urllib.request.urlopen(request) as res:
print(res.read().decode('utf-8'))
def get_googledrive_list(access_token: str):
# List children of the root folder
url = "https://www.googleapis.com/drive/v2/files/root/children"
headers = {'Authorization': 'Bearer ' + access_token}
request = urllib.request.Request(url, method="GET", headers=headers)
with urllib.request.urlopen(request) as res:
dat = json.loads(res.read().decode('utf-8'))
if "items" in dat:
for item in dat["items"]:
print(f"ID: {item['id']} Name: {item['name']}")
get_googledrive_item(access_token, item["id"])
if __name__ == '__main__':
access_token = "YOUR_ACCESS_TOKEN"
get_googledrive_list(access_token)