About the reason why I decided to make this. I decided to run a little batch processing on the Raspberry Pi, but when I was running the server for a long time, not just the Raspberry Pi, the server stopped unknowingly. There can be an event. Also, although the server itself is working, it is possible that batch processing will start moss for some reason, but it is troublesome to log in and check every day. So, I was happy to be able to notify the smartphone of the result of whether the batch processing is normal or abnormal on the Raspberry Pi. If there is no notification, it can be determined that something went wrong with the server. One way to implement it is
-Access Python Quickstart and do the following: --Click the "Enable the Google Sheets API" button and enter an appropriate project name. Save "credentials.json" locally --Install the dedicated library with the following command.
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Regarding the preparation for posting, it is recommended because it is organized in an easy-to-understand manner in past articles. https://qiita.com/connvoi_tyou/items/7cd7ffd5a98f61855f5c
Create a Spreadsheet on Google Drive in advance. Make a note of part of the file URL as you will need to write it in your program.
https://docs.google.com/spreadsheets/d/<XXXXXXXXXXXXXXXXXXX>/edit#gid=zzzzzzzz
# "<XXXXXXXXXXXXXXXXXXX>"Make a note of
To Google Spreadsheet as below I made it with the name "server_check.py".
server_check.py
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
#So far, it is almost the same as the one described in the guide provided by google
#Below, fill in the information specific to Spreadsheet.
spreadsheet_id = '<XXXXXXXXXXXXXXXXXXX>'
sheetname='Sheet 1'
range_ = sheetname
#Get date information
import datetime
_str_dt=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
#Substitute data to write to columns A, B, and C
#Column A is the date, columns B and C are arbitrary character strings(This time, substitute a test string)
values = [
[_str_dt,"Status-1","Status-2"]
]
body = {
'values' : values
}
#Write to Spreadsheet
result=service.spreadsheets().values().append(spreadsheetId=spreadsheet_id,valueInputOption='RAW',range=range_,body=body).execute()
print(result)
In the above program, the test message is something like "Status-1". When actually operating it, extract the output of the log file and I think it will be added to the values array.
After fixing the necessary parts, execute the above python program. ** At that time, the "credentials.json" file must exist in the same directory. ** ** Also, when the program is executed for the first time, browser authentication is required, so it must be executed in the GUI environment.
When executed, the browser will be launched and the confirmation page for the google account will be displayed. Grant access rights according to the screen. (On the way, a warning to unsafe pages will be displayed, but it will continue) When a character like "The authentication flow has completed. You may close this window." Is output to the browser, the setting is complete.
When you've granted access, you should have an entry in your Spreadsheet (with a value in the first row). Also, a "token.pickle" file will be created in the same directory as the program. With this file, the next time you SKIP your browser authentication, the line will be added.
The ultimate goal this time was to run the target program from the Raspberry Pi, but I did not have a GUI environment on the Raspberry Pi (usually working with an SSH connection). Even if I execute "server_check.py" on the Raspberry Pi, the browser cannot be opened, so I cannot set the permissions.
In such a case, if you run the same program in another GUI environment and place the generated "token.pickle" file on the target server, you should be able to run the program on the CUI as well. I can now run it from my Raspberry Pi by running the same program on my Mac and sending the generated token.pickle to my Raspberry Pi.
Occasionally, when I ran the program, I got an error.
[root@localhost python]# python server_check.py
Traceback (most recent call last):
File "server_check.py", line 17, in <module>
creds.refresh(Request())
File "/usr/local/lib/python3.7/site-packages/google/oauth2/credentials.py", line 182, in refresh
self._scopes,
File "/usr/local/lib/python3.7/site-packages/google/oauth2/_client.py", line 248, in refresh_grant
response_data = _token_endpoint_request(request, token_uri, body)
File "/usr/local/lib/python3.7/site-packages/google/oauth2/_client.py", line 124, in _token_endpoint_request
_handle_error_response(response_body)
File "/usr/local/lib/python3.7/site-packages/google/oauth2/_client.py", line 60, in _handle_error_response
raise exceptions.RefreshError(error_details, response_body)
google.auth.exceptions.RefreshError: ('invalid_scope: Some requested scopes were invalid. {invalid=[a, c, d, e, g, h, i, l, m, ., /, o, p, r, s, t, u, w, :]}', '{\n "error": "invalid_scope",\n "error_description": "Some requested scopes were invalid. {invalid\\u003d[a, c, d, e, g, h, i, l, m, ., /, o, p, r, s, t, u, w, :]}",\n "error_uri": "http://code.google.com/apis/accounts/docs/OAuth2.html"\n}')
[root@localhost python]#
I'm not sure what the error was, but I solved it by recreating "token.pickle". To recreate it, just delete "token.pickle" and run the program. However, please be careful about the CUI environment where you cannot open the browser because it will be set from the browser again.
By the way, I don't know why it happens, but from a personal point of view, I feel that an error occurred at the following timing.
--When the program is significantly renovated --When you execute another program and authenticate
With this, I started to notice early when batch processing was abnormal or when the server stopped, but I'm a little tired of LINE coming every day. Actually, I wanted to notify LINE when an abnormality was detected (the server detected an abnormality or there was no regular contact) in the style of "no news is a healthy proof", but that is the next issue. So that's it.
Recommended Posts