2017-10-10 157 views
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甚至更好..它的文件夾(目標),並授權給特定的用戶(通過電子郵件)?

回答

0

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

+0

我認爲這是關係到這一行:憑證= ServiceAccountCredentials.from_json_keyfile_name(「credentials.json」,範圍)。它創建工作表,但它說我沒有授予訪問權限! ( –

+0

)我在腳本中添加了一些行,用於更改文件的權限。首先,我更改JSON(證書)中給出的電子郵件地址的權限,然後對「真實」電子郵件執行相同的操作。 –