📜  Google Workspace API –使用G Suite API

📅  最后修改于: 2021-04-17 02:08:01             🧑  作者: Mango

我们大多数人都熟悉各种Google Workspace产品(也称为G Suite),例如Calendar,Drive和Gmail等。但是除了这些产品之外,Google还提供API来访问G Suite产品以尽可能地构建您自己的服务根据您的需求自定义它们。

在本文中,我们将研究如何使用G Suite REST API和代码示例。为此,您需要了解以下两件事:

  1. 获取G Suite API的凭据
  2. 使用G Suite API创建项目

我们将从Google云端硬盘开始。这是列出您Google云端硬盘中前100个文件和文件夹的代码。

Python3
fro __future__ import print_fnction
from googleclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
  
SCOPES = 'http://www.googleapis.com/auth/drive/drive.metadata.readonly'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
  flow = client.flow_from_clientsecrets('client_secrets.json')
  creds = tools.run_flow(flow, store)
    
DRIVE = discovery.build('drive', 'v3', http = creds.authorize(Http()))
files = Drive.files().list().execute().get('files', [])
for f in files:
  print(f['name'], f['mimeType'])


Python3
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.auth_oauthlib.flow import InstalledAppFlow
from googleapiclient import discovery
  
creds = None
SCOPES = 'http://www.googleapis.com/auth/drive/drive.metadata.readonly'
  
#store & refresh tokens
TOKENS = 'token.p'
  
if os.path.exists(TOKENS):
  with open(TOKENS, 'rb') as token:
    creds = pickle.load(token)
if not (creds and creds.valid):
  if creds and creds.expired and creds.refres_token:
    creds.refresh(Request())
  else:
    flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)
    creds = flow.run_local_server()
with open(TOKENS, 'wb') as token:
  pickle.dump(creds, token)
    
DRIVE = discovery.build('drive', 'v3', http = creds.authorize(Http()))
files = Drive.files().list().execute().get('files', [])
for f in files:
  print(f['name'], f['mimeType'])


真正的应用程序实际上只是最后三行。其他所有内容仅是安全性样板,所请求的作用域和API端点创建。一旦有了Drive API的端点,就如同创建文件列表查询,执行该查询并显示结果一样简单。

Google正在升级到较新的客户端库,以使GCP和G Suite的世界更加紧密地联系在一起。当前的库将在一段时间内保持运行状态,但是您需要查看代码是如何更改的。因此,这是完全相同的示例,但带有更新的库。

Python3

from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.auth_oauthlib.flow import InstalledAppFlow
from googleapiclient import discovery
  
creds = None
SCOPES = 'http://www.googleapis.com/auth/drive/drive.metadata.readonly'
  
#store & refresh tokens
TOKENS = 'token.p'
  
if os.path.exists(TOKENS):
  with open(TOKENS, 'rb') as token:
    creds = pickle.load(token)
if not (creds and creds.valid):
  if creds and creds.expired and creds.refres_token:
    creds.refresh(Request())
  else:
    flow = InstalledAppFlow.from_client_secrets_file('client_secret.json', SCOPES)
    creds = flow.run_local_server()
with open(TOKENS, 'wb') as token:
  pickle.dump(creds, token)
    
DRIVE = discovery.build('drive', 'v3', http = creds.authorize(Http()))
files = Drive.files().list().execute().get('files', [])
for f in files:
  print(f['name'], f['mimeType'])
    
   

如您所见,除了几个导入更改之外,您可以自己管理OAuth令牌,其他所有内容几乎都保持不变。 G Suite文档已经反映了这一变化,但请记住,大多数代码仍然使用原始库。当此示例在Python,您可以猜测客户端库这样升级,无论您使用哪种语言,都可以应用。

使用Drive API的一个原因可能是,您想要编写一个可备份zip文件但将其扩展到Google Drive的应用程序。再举一个例子,假设您在一家初创公司找到一份工作,该公司可以帮助人们使相册自动化。而且,您希望所有休假回来的人都必须将相机和手机清空到外部硬盘驱动器上。如果您使用Drive API访问文件元数据(例如时间戳和地理位置),则您和您的团队实际上可以构建一个可自动生成相册的应用程序。现在更进一步,将这些照片拼接成视频,使用YouTube API上传,然后使用Gmail API告诉您的朋友和家人。

每个Google API都有官方文档。无论API名称是什么,G Suite都可以位于developers.google.com/上,例如Drive。通过“指南”标签构建文档,该标签具有多种语言的快速入门以及特定API功能的指南。

现在,除了快速入门之外,还有更多完善的示例应用程序。这些都位于每个API的示例标签下,并在此处提供了指向开放源代码存储库或开发人员视频的链接。

现在,如果您需要帮助,“支持”选项卡将链接到堆栈溢出,或链接到bug和功能请求的跟踪器,或者是志趣相投的开发人员社区。