📅  最后修改于: 2023-12-03 15:31:03.126000             🧑  作者: Mango
Google云端资料库服务(Google Drive API)是Google提供的云端存储服务,它允许程序员访问Google云端资料库中存储的文件和文件夹。使用Google云端资料库服务,可以轻松地创建、管理和共享云端文件,并集成到自己的应用程序中。
Google云端资料库服务提供了丰富的功能,包括:
使用Google云端资料库服务需要进行如下步骤:
以下是一个使用Python接口访问Google云端资料库服务API的示例代码:
#引入相关库和模块
import os
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
#定义API信息和凭据
API_NAME = 'drive'
API_VERSION = 'v3'
CREDENTIALS_PATH = './credentials.json'
SCOPE = ['https://www.googleapis.com/auth/drive.file']
#读取凭据文件并获取授权
if os.path.exists(CREDENTIALS_PATH):
creds = Credentials.from_authorized_user_file(CREDENTIALS_PATH, SCOPE)
else:
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_PATH, SCOPE)
creds = flow.run_local_server(port=0)
with open(CREDENTIALS_PATH, 'w') as token:
token.write(creds.to_json())
#连接API并调用方法
service = build(API_NAME, API_VERSION, credentials=creds)
try:
file_metadata = {'name': 'Hello World'}
file = service.files().create(body=file_metadata, media_body='hello.txt', fields='id').execute()
print('File ID: %s' % file.get('id'))
except HttpError as error:
print('An error occurred: %s' % error)