简体   繁体   English

通过云功能访问Google Sheets Python API的身份验证问题

[英]Authentication issue accessing Google Sheets Python API via cloud functions

I am trying to access a Google sheet using a cloud function and the sheets API in python, but get a 403 permissions error. 我尝试使用python中的云功能和工作表API访问Google工作表,但出现403权限错误。 I have created a service account, exported the key (included in the cloud function zip I am uploading) and have given this service account the API scopes necessary to access sheets (I've tried both https://www.googleapis.com/auth/spreadsheets and https://www.googleapis.com/auth/drive ). 我已经创建了一个服务帐户,导出了密钥(包含在我正在上传的云功能zip中),并为该服务帐户提供了访问工作表所需的API范围(我已经尝试过https://www.googleapis.com/ auth / spreadsheetshttps://www.googleapis.com/auth/drive )。 Here is the code: 这是代码:

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

def main(request):
  scope = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive']

  credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secrets.json', scope)

  service = build('sheets', 'v4', http=credentials.authorize(httplib2.Http()))

  spreadsheet_id = 'id-of-the-google-sheet'

  range = 'Sheet1!A:D'

  response = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range).execute()

  cols = response.get('values', [])[0]
  return cols[0]

requirements.txt: requirements.txt:

google-api-python-client
oauth2client
google-auth-httplib2
google-auth

If I explicitly share the Google sheet with the service account, the authentication does work and I can access the sheet data. 如果我与服务帐户明确共享Google工作表,则身份验证确实有效,并且可以访问工作表数据。 The question is why doesn't this work by applying the proper API scopes as the Google accounts admin? 问题是,为什么不以Google帐户管理员的身份应用适当的API范围而无法正常工作?

Google Drive permissions are on top of scopes. Google云端硬盘权限是最重要的。 Scopes are needed for authentication of the request, but Drive permissions (and Sheets by extension) are what determines who has access to which file. 范围是进行请求身份验证所必需的,但是驱动器权限(和扩展表)是确定谁可以访问哪个文件的权限。 If the calling account (the serviceaccount in this case) doesn't have access to the Sheet in question you'll get the 403 you've been getting. 如果主叫帐户(在这种情况下为serviceaccount)无法访问所涉及的工作表,则将获得您已经获得的403。

Setting up the scopes in the Admin Console doesn't have direct correlation to the permissions. 在管理控制台中设置作用域与权限没有直接关系。

One thing to note as well, if you have documents shared to everyone in the G Suite account by default, you could use Domain Wide Delegation (1) and using the serviceaccount impersonate a user (like the superadmin) inside the G Suite domain. 同样要注意的一件事是,如果默认情况下G Suite帐户中的每个人都共享文档,则可以使用域范围委派(1)并使用serviceaccount来模拟G Suite域内的用户(例如超级管理员)。 You wouldn't need to share each document with the serviceaccount itself. 您无需与服务帐户本身共享每个文档。

(1) This one is for the Admin SDK , but this is the article that contains the best example. (1) 这是针对Admin SDK的 ,但这是包含最佳示例的文章。 You're interested in the credentials object there. 您对那里的凭证对象感兴趣。 You can also create the object with a .json IIRC. 您还可以使用.json IIRC创建对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM