创建谷歌电子表格Python3不开(权限)
问题描述:
我已成功地打开和使用谷歌API编辑现有的谷歌片,但是当我尝试创建一个新的我会失败:创建谷歌电子表格Python3不开(权限)
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'}
有没有一种方法来解析spreadsheetId甚至更好..它的文件夹(目标),并授权给特定的用户(通过电子邮件)?
答
您可以使用此Method: spreadsheets.create来创建电子表格。
有一个可用的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
代码来处理response
字典: pprint(响应)
对于授权,则可以使用利用了以下OAuth范围之一:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/spreadsheets
有关授权的更多信息,你可以访问这个Auth Guide
我认为这是关系到这一行:凭证= ServiceAccountCredentials.from_json_keyfile_name(“credentials.json”,范围)。它创建工作表,但它说我没有授予访问权限! ( –
)我在脚本中添加了一些行,用于更改文件的权限。首先,我更改JSON(证书)中给出的电子邮件地址的权限,然后对“真实”电子邮件执行相同的操作。 –