0
我已成功地打開和使用谷歌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甚至更好..它的文件夾(目標),並授權給特定的用戶(通過電子郵件)?
我認爲這是關係到這一行:憑證= ServiceAccountCredentials.from_json_keyfile_name(「credentials.json」,範圍)。它創建工作表,但它說我沒有授予訪問權限! ( –
)我在腳本中添加了一些行,用於更改文件的權限。首先,我更改JSON(證書)中給出的電子郵件地址的權限,然後對「真實」電子郵件執行相同的操作。 –