简体   繁体   English

在Google App Engine云端点内部调用Drive API

[英]Make calls to Drive API inside of a Google App Engine Cloud Endpoints

I am trying to build my own endpoints inside of an App Engine Application. 我正在尝试在App Engine应用程序内部构建自己的端点。 There is an endpoint API that needs to ask user for " https://www.googleapis.com/auth/drive.readonly " scope. 有一个终结点API需要向用户询问“ https://www.googleapis.com/auth/drive.readonly ”范围。 It performs a list of the Drive API and scan the drive file of that user. 它执行Drive API的列表并扫描该用户的驱动器文件。

The problem is that I don't know how to make call to Drive api inside of an endpoint API. 问题是我不知道如何在端点API内调用Drive api。

I think inside of the endpoint method, it has the credentials we got from the user. 我认为在端点方法内部,它具有我们从用户那里获得的凭据。 But I don't know how to receive that. 但是我不知道该如何接受。

I am using python as the backend language. 我正在使用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()

You can use os.environ to access the HTTP Authorization header, that includes the access token, that was granted all scopes you asked for on the client side, include drive.readonly in your sample. 您可以使用os.environ访问HTTP授权标头,其中包括访问令牌,该令牌已被授予您在客户端要求的所有作用域, drive.readonly在示例中包含drive.readonly

if "HTTP_AUTHORIZATION" in os.environ:
  (tokentype, token)  = os.environ["HTTP_AUTHORIZATION"].split(" ")

You can then use this token to make calls to the API, either directly or by using the Google APIs Client Library for Python : 然后,您可以直接或通过使用适用于PythonGoogle API客户端库,使用此令牌来调用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()

Note that this approach won't work if you are using an Android client, because that uses ID-Token authorization instead of access tokens. 请注意,如果您使用的是Android客户端,则此方法将不起作用,因为它使用ID-Token授权而不是访问令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM