TL; DR
--I wrote how to unattended Google Spreadsheets tables using a Python program. -Official Quick Start is a human-mediated OAuth2 method, but it's an unmanned version of this. --Create a service account, give it the required permissions, and use the account's private key to access it.
This is nothing special. Create a suitable sheet in Google Drive and make a note of the file ID.
Of course, you need permission to operate various documents. When operating from a program, there are roughly two methods for acquiring authority.
--Temporarily obtain permission from a human and operate with that human account --Operate with a privileged ** machine account **
Roughly speaking, the former is the method used in interactive software, and the latter is the method used in automated systems. This time we will use the latter method. Here, the "machine account" is called ** service account **. In other words, you first need to create a service account and give it the required permissions. The procedure is roughly as follows.
[email protected]
.Google Sheets API
.Maybe this is all you need.
$ pip3 install google-api-python-client google-auth-oauthlib oauth2client
The code to write test in cell A1 of the sheet. Only this!
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
PRIVKEY_FILE = "projectname-*********.json" #Downloaded private key
SPREADSHEET_ID = "********" #Sheet file ID
SCOPE = ["https://www.googleapis.com/auth/spreadsheets"]
creds = ServiceAccountCredentials.from_json_keyfile_name(PRIVKEY_FILE, SCOPE)
service = build("sheets", "v4", credentials=creds)
sheet = service.spreadsheets()
result = sheet.values().update(
spreadsheetId=SPREADSHEET_ID,
range="a1",
valueInputOption="RAW", # USER_You can enter an expression as an expression with ENTERED(Example: "=sum(a1:a100)")
body={"values": [["test"]]}).execute()
It was just as easy with Ruby.
Recommended Posts