How to use Oauth2 service accounts with Google Drive and the App Engine SDK

不想你离开。 提交于 2019-12-07 16:53:56

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/14669661/how-to-use-oauth2-service-accounts-with-google-drive-and-the-app-engine-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!