我目前有代码可以将数据上传到Google云端硬盘中的Google表格中。但是,我想让其他用户访问此Google表格。我尝试了一些在网上找到的代码,以授予其他用户访问的权限,包括在下面,但是该代码给了我一个错误,也包含在下面。
这是我的代码:
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
#drive_api = build('drive', 'v3', credentials=credentials)
# Please set worksheet names.
sheetNameForWorksheet1 = "Report Overview"
sheetNameForWorksheet2 = "Error Details"
sheetNameForWorksheet3 = "% of missing values per column"
# Please set values for each worksheet. Values are 2 dimensional array.
valuesForWorksheet1 = overview_df.T.reset_index().T.values.tolist()
valuesForWorksheet2 = details_df.T.reset_index().T.values.tolist()
valuesForWorksheet3 = miss_occ_df.T.reset_index().T.values.tolist()
spreadsheet = {
'properties': {
'title': 'Data Integrity Report Completed on {}'.format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
},
"sheets": [
{
"properties": {
"title": sheetNameForWorksheet1
}
},
{
"properties": {
"title": sheetNameForWorksheet2
}
},
{
"properties": {
"title": sheetNameForWorksheet3
}
}
]
}
spreadsheet = service.spreadsheets().create(body=spreadsheet, fields='spreadsheetId').execute()
gsheet_id = spreadsheet.get('spreadsheetId')
batch_update_values_request_body = {
"data": [
{
"values": valuesForWorksheet1,
"range": sheetNameForWorksheet1,
},
{
"values": valuesForWorksheet2,
"range": sheetNameForWorksheet2,
},
{
"values": valuesForWorksheet3,
"range": sheetNameForWorksheet3,
}
],
"valueInputOption": "USER_ENTERED"
}
response = service.spreadsheets().values().batchUpdate(spreadsheetId=gsheet_id, body=batch_update_values_request_body).execute()
我在网上找到了此代码并进行了尝试:
drive = build('drive', 'v3')
domain_permission = {
'type': 'domain',
'role': 'writer',
'emailAddress': 'example@gmail.com',
# Magic almost undocumented variable which makes files appear in your Google Drive
'allowFileDiscovery': True,
}
req = drive.permissions().create(fileId=gsheet_id, body=domain_permission, fields="id")
req.execute()
但是,这给了我以下错误:
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started
该如何解决?
答案 0 :(得分:1)
如果我的理解正确,那么这个答案如何?
google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据。请设置GOOGLE_APPLICATION_CREDENTIALS或明确创建凭据,然后重新运行该应用程序。有关更多信息,请参见https://cloud.google.com/docs/authentication/getting-started
上述错误消息的原因是drive = build('drive', 'v3')
。在这种情况下,不包括使用API的访问令牌。因此,请进行如下修改。
drive = build('drive', 'v3')
至:
drive = build('drive', 'v3', credentials=creds)
Request had insufficient authentication scopes.
错误,请执行以下流程。
https://www.googleapis.com/auth/drive
一样将SCOPE
添加到SCOPES = ["https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/drive"]
。token.pickle
文件并通过运行脚本来授权范围。
此修改后的脚本假设domain_permission
的对象设置正确。
如果要允许以用户身份进行编辑,请尝试以下对象。
domain_permission = {
'type': 'user',
'role': 'writer',
'emailAddress': 'example@gmail.com'
}
如果我误解了您的问题,而这不是您想要的结果,我深表歉意。如果发生其他错误,您可以显示错误消息吗?还可以提供整个脚本吗?由此,我想确认一下。当然,请删除您的个人信息。