使用 API 使用 Python3 创建了 google 电子表格无法打开(权限)

Created google spreadsheet with Python3 using the API does not open (permissions)

我已经设法使用 Google API 打开和编辑现有的 google sheet,但是当我尝试创建一个新的时我失败了:

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)

上面创建了一个文件,但是当我访问终端中给出的 link 时,我需要授予访问权限!终端的输出如下:

{'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'}

有没有办法解析传播sheetId 或更好..它的文件夹(目的地)并授予特定用户访问权限(通过电子邮件)?

您可以使用此 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