简体   繁体   English

在公司限制的电子表格上使用谷歌表 API 检索谷歌表值

[英]Retrieving google sheet values with google sheet api on company restricted spreadsheet

I want to be able to read a spreadsheet, that has been shared with me, inside the company I work.我希望能够阅读我工作的公司内部与我共享的电子表格。 I have tried adapting the script from this link .我已经尝试从这个链接改编脚本。 This is the code I use:这是我使用的代码:

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

scope = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive'
]

SERVICE_ACCOUNT_FILE = "gsheetread-293005-d7e75122e4c7.json"

credentials = ServiceAccountCredentials.from_json_keyfile_name(
    SERVICE_ACCOUNT_FILE, scopes=scope)

# Use the create_delegated() method and authorize the delegated credentials
delegated_credentials = credentials.create_delegated('myemail@company.com')
delegated_http = delegated_credentials.authorize(httplib2.Http())

google_sheet = discovery.build('spreadsheet_id', 'v3', http=delegated_http)

I get an error:我收到一个错误:

oauth2client.client.HttpAccessTokenRefreshError: unauthorized_client: Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested. oauth2client.client.HttpAccessTokenRefreshError:未授权的客户端:客户端无权使用此方法检索访问令牌,或客户端未获得任何请求范围的授权。

The error you're getting means you didn't add some or all of the specified scopes ( https://www.googleapis.com/auth/spreadsheets , https://www.googleapis.com/auth/drive ) when granting domain-wide authority to your service account.您收到的错误意味着您在授予时没有添加部分或全部指定范围( https://www.googleapis.com/auth/spreadsheetshttps://www.googleapis.com/auth/drive )您的服务帐户的域范围权限。 Please follow these steps :请按照以下步骤操作

在此处输入图片说明

In step 5, you have to add both https://www.googleapis.com/auth/spreadsheets , https://www.googleapis.com/auth/drive (actually, you only need to add one, if you're just accessing a spreadsheet, but you should remove the other one from your code).在第 5 步中,您必须同时添加https://www.googleapis.com/auth/spreadsheetshttps://www.googleapis.com/auth/drive (实际上,您只需要添加一个,如果您是只是访问电子表格,但您应该从代码中删除另一个)。

Additional notes:补充说明:

  • The library you're using, oauth2client , is deprecated .您正在使用的库oauth2client 已弃用 Use google-auth instead, which doesn't rely on httplib2 .改用google-auth ,它不依赖于httplib2
  • You're not using a valid service name ( spreadsheet_id ).您没有使用有效的服务名称 ( spreadsheet_id )。 If you want to use Sheets API, the service name should be sheets .如果你想使用 Sheets API,服务名称应该是sheets
  • Sheets API V3 is deprecated and will be shut down in January 2021. Use V4 instead. Sheets API V3已弃用,将于 2021 年 1 月关闭。请改用V4
  • As I mentioned before, there's no need to use the drive scope if you're accessing a spreadsheet.正如我之前提到的,如果您正在访问电子表格,则无需使用drive范围。 spreadsheets is enough. spreadsheets就足够了。

Code snippet (using non-deprecated library):代码片段(使用未弃用的库):

from googleapiclient.discovery import build
from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SERVICE_ACCOUNT_FILE = "gsheetread-293005-d7e75122e4c7.json"

creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
delegated_credentials = creds.with_subject('myemail@company.com')
service = build('sheets', 'v4', credentials=delegated_credentials)
spreadsheet = service.spreadsheets().get(spreadsheetId="YOUR_SPREADSHEET_ID").execute()
print(spreadsheet)

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

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