所以基本上,问题是我想让其他用户将电子表格拖放到共享驱动器中,并希望通过python API访问这些文件
错误消息“此文档不支持此操作”
JSON响应
{ “错误”:{ “代码”:403, “ message”:“该请求缺少有效的API密钥。”, “状态”:“ PERMISSION_DENIED” } }
我正在使用python创建文件夹和工作表,我想共享该文件夹供其他用户更新电子表格文件,以便我向其他用户授予权限,但是当他们将文件放到驱动器中时,我可以访问该文件元数据,而不是电子表格的内容
`SCOPES = ['https://www.googleapis.com/auth/drive.appdata','https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive']
sheet_service = build('sheets', 'v4', credentials=get_Service())
drive_service = build('drive', 'v3',credentials=get_Service())
def get_values(self,SAMPLE_SPREADSHEET_ID, SAMPLE_RANGE_NAME):
sheet = sheet_service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=SAMPLE_RANGE_NAME).execute()`
Expected help > bypass these permission restrictions
当我放下文件时,我注意到某些事情,工作表ID与使用API或通过Google驱动器创建工作表ID有所不同
答案 0 :(得分:0)
{ "error": { "code": 403, "message": "The request is missing a valid API key.", "status": "PERMISSION_DENIED" } }
意味着您没有随请求发送任何授权。您必须先授权您的请求,然后才能执行任何操作。请注意以下代码中如何将凭据添加到服务变量中。
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
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
SAMPLE_RANGE_NAME = 'Class Data!A2:E'
def main():
"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
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)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=SAMPLE_RANGE_NAME).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
print('Name, Major:')
for row in values:
# Print columns A and E, which correspond to indices 0 and 4.
print('%s, %s' % (row[0], row[4]))
if __name__ == '__main__':
main()
从Python quickstart翻录的代码
还请注意,您无法使用Sheets api在Google驱动器中创建文件夹,您将需要使用Google驱动器api创建文件。表格只能让您对其进行编辑。
将文件上传到Google云端硬盘时,您需要Import doc types将其转换为工作表
file_metadata = {
'name': 'My Report',
'mimeType': 'application/vnd.google-apps.spreadsheet'
}
media = MediaFileUpload('files/report.csv',
mimetype='text/csv',
resumable=True)
file = drive_service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
print 'File ID: %s' % file.get('id')