我正在尝试在 App Engine 应用程序内构建自己的端点。 有一个端点 API 需要向用户询问 “https://www.googleapis.com/auth/drive.readonly”范围。 它执行 Drive API 列表并扫描该用户的驱动器文件。
问题是我不知道如何在端点 API 内调用 Drive api。
我认为在端点方法内部,它具有我们从用户那里获得的凭据。但我不知道如何接收它。
我使用 python 作为后端语言。
@drivetosp_test_api.api_class(resource_name='report')
class Report(remote.Service):
@endpoints.method(EmptyMessage, EmptyMessage,
name='generate',
path='report/generate',
http_method='GET'
)
def report_generate(self, request):
logging.info(endpoints)
return EmptyMessage()
最佳答案
您可以使用 os.environ
访问 HTTP 授权 header ,其中包括访问 token ,该 header 已被授予您在客户端请求的所有范围,包括 drive.readonly
在您的示例中。
if "HTTP_AUTHORIZATION" in os.environ:
(tokentype, token) = os.environ["HTTP_AUTHORIZATION"].split(" ")
然后,您可以使用此 token 直接或通过使用 Google APIs Client Library for Python 来调用 API :
credentials = AccessTokenCredentials(token, 'my-user-agent/1.0')
http = httplib2.Http()
http = credentials.authorize(http)
service = build('drive', 'v2', http=http)
files = service.files().list().execute()
请注意,如果您使用 Android 客户端,此方法将不起作用,因为它使用 ID-Token 授权而不是访问 token 。
关于python - 调用 Google App Engine Cloud Endpoints 内的 Drive API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26224361/