googleapi/Python中的授权访问

googleapi/Python中的授权访问,python,google-drive-api,google-api-python-client,Python,Google Drive Api,Google Api Python Client,因此,我有一个Python应用程序正在运行,它在我们的域中使用一个服务帐户。这一切工作正常,服务帐户已被授予访问正确范围的权限。我使用的是从谷歌的一个例子中摘取的以下内容: from __future__ import print_function import httplib2 import os import pprint import sys from apiclient.discovery import build from oauth2client.service_account i

因此,我有一个Python应用程序正在运行,它在我们的域中使用一个服务帐户。这一切工作正常,服务帐户已被授予访问正确范围的权限。我使用的是从谷歌的一个例子中摘取的以下内容:

from __future__ import print_function
import httplib2
import os
import pprint
import sys

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

"""Email of the Service Account"""
SERVICE_ACCOUNT_EMAIL = 'service_account_email@google'

"""Path to the Service Account's Private Key file"""
SERVICE_ACCOUNT_CLIENT_FILE_PATH = 'My Project-xxxxxx.json'

def main():
  scopes = ['https://www.googleapis.com/auth/drive.metadata.readonly']
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
            SERVICE_ACCOUNT_CLIENT_FILE_PATH,
            scopes=scopes
        )

  http = httplib2.Http()

  http = credentials.authorize(http)

  service = build('drive', 'v3', http=http)

  results = service.files().list(
      pageSize=10,fields="nextPageToken, files(id, name)").execute()
  items = results.get('files', [])
  if not items:
      print('No files found.')
  else:
      print('Files:')
      for item in items:
          print('{0} ({1})'.format(item['name'], item['id']))

if __name__ == '__main__':
    main()
这将成功检索服务帐户的文档。现在我的理解是,我应该能够委派访问权限,这样我就可以作为另一个用户运行。因此,我将添加以下行:

delegated_credentials = credentials.create_delegated("user.name@org_domain.org.au")
然后在授权时使用DeleteU凭据。在这一点上,我得到了错误

oauth2client.client.HttpAccess令牌刷新错误:访问被拒绝: 请求的客户端未授权


所以我的假设是,我指定的用户没有访问API的权限。这是正确的方法还是我遗漏了一些明显的东西?

发现了我的错误,在这里发布给后代。我在python代码中的作用域不正确,没有意识到它需要与管理客户端中授予的作用域完全匹配

管理客户端中的范围如下所示

守则的范围是现在,

scopes = ['https://www.googleapis.com/auth/admin.reports.audit.readonly','https://www.googleapis.com/auth/drive']
我知道我不需要报告的范围,但问题是如果它们不匹配,那么它就不起作用


新手失误

非常感谢这篇文章,但是你如何找到“管理客户端中的范围”?具体在哪一页?我已经有一段时间没有看到这个了,但是我认为它在开发人员控制台的页面下。