我试图在App应用程序中构建自己的端点。有一个端点API需要向用户询问"https://www.googleapis.com/auth/drive.readonly“作用域。它执行一个驱动器API列表,并扫描该用户的驱动器文件。
问题是,我不知道如何在端点API中调用驱动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()
发布于 2014-10-06 12:50:03
您可以使用os.environ
访问,该头包含访问令牌,它被授予了客户端请求的所有作用域,包括示例中的drive.readonly
。
if "HTTP_AUTHORIZATION" in os.environ:
(tokentype, token) = os.environ["HTTP_AUTHORIZATION"].split(" ")
然后可以使用此令牌直接或通过使用用于Python的Google客户端库对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令牌授权而不是访问令牌。
https://stackoverflow.com/questions/26224361
复制