如何在Google Drive和App Engine SDK中使用Oauth2服务帐户 [英] How to use Oauth2 service accounts with Google Drive and the App Engine SDK

查看:166
本文介绍了如何在Google Drive和App Engine SDK中使用Oauth2服务帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用App Engine和服务帐户访问Google云端硬盘可以正常工作。为了使它工作,我还必须与服务帐户名称appid@appspot.gserviceaccount.com分享我的云端硬盘文件。



但是当我使用GAE SDK I收到401.我认为原因是我的股票的SDK的service_account名称。 SDK的服务帐户名是:test @ localhost根据:

  app_identity.get_service_account_name()#返回test @ localhost 

SDK控制台中的错误消息:

发生错误:https://www.googleapis.com/drive/v2/files/0B9-mEÝ....nM/children?alt= json& key = AI ........................ N0返回Invalid Credentials>



这是访问Google云端硬盘的代码:

  def get(self):

SCOPE = 'https://www.googleapis.com/auth/drive'
credentials = AppAssertionCredentials(scope = SCOPE)
logging.info(app_identity.get_service_account_name())
http = credentials.authorize (httplib2.Http())
如果os.environ ['SERVER_SOFTWARE']。startswith('Development'):
service = build('drive','v2',http = http,developerKey = '... API key ...')
else:
service = buil d('drive','v2',http = http,developerKey ='... APY key ...')
#驱动请求

更新:



我的个人观点:这只能用于GAE, SDK,因为我无法使用SDK service_account名称创建Drive共享。或者可以指定另一个服务帐户名称。

解决方案

请检查应用程序拥有的服务帐户,还有一个

文档包含Python示例代码。


Accessing Google Drive with App Engine and service accounts works fine. To make it work, I also had to share my Drive files with the Service Account Name: appid@appspot.gserviceaccount.com.

But when I use the GAE SDK I receive a 401. I think the reason is the service_account name of the SDK for my shares. The service account name of the SDK is: test@localhost according to :

app_identity.get_service_account_name()  # returns test@localhost

The error message in the SDK console :

An error occurred: https://www.googleapis.com/drive/v2/files/0B9-mE....................nM/children?alt=json&key=AI........................N0 returned "Invalid Credentials">

This is the code to access Google Drive :

def get(self):

    SCOPE = 'https://www.googleapis.com/auth/drive'
    credentials = AppAssertionCredentials(scope=SCOPE)
    logging.info(app_identity.get_service_account_name())
    http = credentials.authorize(httplib2.Http())
    if os.environ['SERVER_SOFTWARE'].startswith('Development') : 
        service = build('drive', 'v2', http=http, developerKey='...API key...')
    else :
        service = build('drive', 'v2', http=http, developerKey='...APY key...')
    # drive requests        

UPDATE :

My personal opinion : This can only work with GAE and not with the SDK, because I cannot make a Drive share with the SDK service_account name. Or is it possible to specify another service account name.

解决方案

Please check the documentation for Application-owned service accounts, and there is also a video you can watch too.

The docs contain Python sample code.

这篇关于如何在Google Drive和App Engine SDK中使用Oauth2服务帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆