[英]Make calls to Drive API inside of a Google App Engine Cloud Endpoints
我正在嘗試在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授權標頭,其中包括訪問令牌,該令牌已被授予您在客戶端要求的所有作用域, drive.readonly
在示例中包含drive.readonly
。
if "HTTP_AUTHORIZATION" in os.environ:
(tokentype, token) = os.environ["HTTP_AUTHORIZATION"].split(" ")
然后,您可以直接或通過使用適用於Python的Google 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()
請注意,如果您使用的是Android客戶端,則此方法將不起作用,因為它使用ID-Token授權而不是訪問令牌。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.