无法使用 Drive API 授权 App Engine

Cannot Authorize App Engine with Drive API

我正在尝试获取 App Engine 上的云端硬盘文件列表。该应用程序不适用于应用程序以外的消费者,因此我没有为用户登录实施 OAuth2。

我在 GCP 项目中启用了 Drive,并且计算实例是所有者。但是,我收到以下错误:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    credentials = app_engine.Credentials(scopes=SCOPES)
File "/usr/local/lib/python2.7/site-packages/google/auth/app_engine.py",     line 105, in __init__
    'The App Engine APIs are not available.')
EnvironmentError: The App Engine APIs are not available.

这是我的代码(基于本教程:https://developers.google.com/api-client-library/python/auth/service-accounts):

from google.auth import app_engine
import googleapiclient.discovery

SCOPES = ['https://www.googleapis.com/auth/drive']
credentials = app_engine.Credentials(scopes=SCOPES)

project_id = 'project-id'

service = googleapiclient.discovery.build('drive', 'v3',credentials=credentials)
bucket = service.files().list(project=project_id).execute()
print bucket

我的凭据出现问题,我不确定是什么。

Tldr;如何授权我的 App Engine Flask 应用(标准版)使用 Google API,包括 Google Sheets、Drive、BigQuery 等。请记住,这是一个服务器到服务器的工作。除了应用程序本身,没有其他用户登录。

最终,我试图让 Google 表格中的值与 BigQuery 同步(该应用需要 Drive 和 BQ 权限),然后将其推送到 DataStore。

如果您有关于如何修复它的建议,ELI5。

查看您正在学习的教程有一个 note that says if you need to test you application locally you should use other credential mechanism。在这种情况下,您必须手动获取并提供服务帐户凭据。 您需要获取包含服务帐户密钥的 JSON 文件,然后在您的代码中通过指定私钥文件来使用服务帐户凭据。 还有另一个好处 tutorial 可能对设置服务器到服务器生产应用程序的身份验证很有用。