【问题标题】:Google Drive Service Account Access on Google App Engine to Impersonate UsersGoogle App Engine 上的 Google Drive 服务帐户访问权限以模拟用户
【发布时间】:2013-08-12 16:55:38
【问题描述】:

我正在尝试获得对 Google Drive API 的服务帐户访问权限。我在构建应用程序时遵循了Google Drive SDK example。我的代码与示例几乎完全一样:

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">

我知道文件 ID 存在,因为我从 UI 复制了它。我正在对变量 API_KEY 使用简单的密钥访问。我应该以不同的方式验证我的申请吗?

EDIT1

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

 cannot import SignedJwtAsserionCredentials 

错误。从那里我确保将 pycrypto 库包含在我的app.yaml 中。有什么想法吗?

EDIT2

我已成功在应用引擎上模拟用户。我关注了this 之前回答的问题,并且成功了。

【问题讨论】:

  • 关键信息是我的“子”参数。谢谢!

标签: python google-app-engine google-drive-api google-api-python-client


【解决方案1】:

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

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) 

【讨论】:

  • 用户在我的代码中是一个变量的原因是因为我正在镜像this example 如何使用域范围的委托并模拟其他用户。
  • 好的。我不熟悉这个选项。
  • 另外,当我使用代码时,我收到了同样的错误(更改 API 密钥后)
  • 当您使用我的代码时,您是否将服务帐户添加到您的文件中以授予其访问权限?
  • 是的,但这不适合我的用例。谢谢你。对用户模拟有任何见解吗?目前,我可以从终端使用 SignedJwtAssertionCredentials 运行我的程序,但由于我希望应用程序在 GAE 上运行,我无法下载密钥。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 2012-09-02
  • 2019-01-20
  • 1970-01-01
  • 2012-03-15
  • 1970-01-01
相关资源
最近更新 更多