Python 3.x 使用API使用Python3创建的google电子表格未打开(权限)
我已使用google API打开并编辑了一个现有的google工作表,但在尝试创建新工作表时失败了:Python 3.x 使用API使用Python3创建的google电子表格未打开(权限),python-3.x,google-drive-api,google-spreadsheet-api,Python 3.x,Google Drive Api,Google Spreadsheet Api,我已使用google API打开并编辑了一个现有的google工作表,但在尝试创建新工作表时失败了: import gspread from oauth2client.service_account import ServiceAccountCredentials from pprint import pprint from googleapiclient import discovery scope = ['https://spreadsheets.google.com/feeds',
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint
from googleapiclient import discovery
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
gc = gspread.authorize(credentials)
service = discovery.build('sheets', 'v4', credentials=credentials)
spreadsheet_body = {
#
}
request = service.spreadsheets().create(body=spreadsheet_body)
response = request.execute()
pprint(response)
上面创建了一个文件,但当我访问终端中给出的链接时,我需要授予访问权限!终端的输出如下:
{'properties': {'autoRecalc': 'ON_CHANGE',
'defaultFormat': {'backgroundColor': {'blue': 1,
'green': 1,
'red': 1},
'padding': {'bottom': 2,
'left': 3,
'right': 3,
'top': 2},
'textFormat': {'bold': False,
'fontFamily': 'arial,sans,sans-serif',
'fontSize': 10,
'foregroundColor': {},
'italic': False,
'strikethrough': False,
'underline': False},
'verticalAlignment': 'BOTTOM',
'wrapStrategy': 'OVERFLOW_CELL'},
'locale': 'en_US',
'timeZone': 'Etc/GMT',
'title': 'Untitled spreadsheet'},
'sheets': [{'properties': {'gridProperties': {'columnCount': 26,
'rowCount': 1000},
'index': 0,
'sheetId': 0,
'sheetType': 'GRID',
'title': 'Sheet1'}}],
'spreadsheetId': '1bSzXveNHTFDuk6Idj5snsZQVp0RAR-ksb5s_CZusQus',
'spreadsheetUrl': 'https://docs.google.com/spreadsheets/d/1bSzXveNHTFDuk6Idj5snsZQVp0RAR-ksb5s_CZusQus/edit'}
有没有一种方法可以解析电子表格ID,或者更好。。它的文件夹(目的地)并授予特定用户访问权限(通过电子邮件)?您可以使用它创建电子表格
您可以使用一个可用的python代码
"""
BEFORE RUNNING:
---------------
1. If not already done, enable the Google Sheets API
and check the quota for your project at
https://console.developers.google.com/apis/api/sheets
2. Install the Python client library for Google APIs by running
`pip install --upgrade google-api-python-client`
"""
from pprint import pprint
from googleapiclient import discovery
# TODO: Change placeholder below to generate authentication credentials. See
# https://developers.google.com/sheets/quickstart/python#step_3_set_up_the_sample
#
# Authorize using one of the following scopes:
# 'https://www.googleapis.com/auth/drive'
# 'https://www.googleapis.com/auth/drive.file'
# 'https://www.googleapis.com/auth/spreadsheets'
credentials = None
service = discovery.build('sheets', 'v4', credentials=credentials)
spreadsheet_body = {
# TODO: Add desired entries to the request body.
}
request = service.spreadsheets().create(body=spreadsheet_body)
response = request.execute()
# TODO: Change
处理响应的代码如下:
警察公共关系科(回应)
对于授权,您可以使用以下OAuth作用域之一:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/spreadsheets
有关授权的更多信息,您可以访问此我认为它与这一行相关:credentials=ServiceAccountCredentials.from_json_keyfile_name('credentials.json',scope)。它创建了工作表,但它说我无权访问!:(我在脚本中添加了更改文件权限的行。首先,我更改JSON(凭据)中给出的电子邮件地址的权限,然后对我的“真实”电子邮件执行相同的操作。