通过Google App Engine上的Google Drive服务帐户访问来模拟用户

问题描述 投票:2回答:1

我正在尝试获得对Google云端硬盘API的服务帐户访问权限。 构建应用程序时,我遵循了Google Drive SDK示例 。 我的代码几乎完全类似于该示例:

class MainPage(webapp2.RequestHandler):
    def get(self):
      build = createDriveService(user)
      searchFile = build.files().get(fileId='FILEID').execute()
      self.response.write(searchFile)

def createDriveService(userEmail):
    API_KEY = 'APIKEY' 
    credentials = AppAssertionCredentials(
        scope='https://www.googleapis.com/auth/drive',
        sub=userEmail)
    http = httplib2.Http()
    http = credentials.authorize(http)

    return build('drive', 'v2', http=http, developerKey=API_KEY)

当我打电话访问我的GAE页面时,我得到的日志中的错误是:

<"File not found: FILEID">

我从UI复制文件ID时就知道它存在。 我正在对变量API_KEY使用简单的Key访问。 我应该以其他方式验证我的申请吗?

编辑1

我尝试了其他各种StackOverflow。 其中之一涉及使用SignedJwtAssertionCredentials并将.p12密钥转换为.pem密钥。 更改之后,我得到了

 cannot import SignedJwtAsserionCredentials 

错误。 从那里,我确保在我的app.yaml包含pycrypto库。 任何想法?

编辑2

我已成功模拟用户使用App Engine。 我遵循先前回答的问题,并且有效。

python google-app-engine google-drive-api google-api-python-client
1个回答
0
投票

我不理解您代码的用户部分,因为您使用的是Google App Engine项目用户帐户。 有关如何使用和查找此帐户的信息,请参阅此文档 。 您也可以使用以下方式找到此帐户:

from google.appengine.api import app_identity
logging.info('service account : ' + app_identity.get_service_account_name())

确保已授予该项目用户帐户访问驱动器文件或文件夹的权限!

我的代码如下所示:

def createDriveService():

    SCOPE = 'https://www.googleapis.com/auth/drive'
    API_KEY = 'AIzaSyB9UkK4OH5Z_E4v3Qp6bay6QEgGpzou3bc'     # GAE
    credentials = AppAssertionCredentials(scope=SCOPE)
    logging.info('using service account : ' + app_identity.get_service_account_name())
    http = credentials.authorize(httplib2.Http())
    return build('drive', 'v2', http=http, developerKey=API_KEY) 
© www.soinside.com 2019 - 2024. All rights reserved.