73 lines
2.9 KiB
Python
73 lines
2.9 KiB
Python
"""
|
|
Copyright © 2023 Alexandre Racine <https://alex-racine.ch>
|
|
|
|
This file is part of Inopy.
|
|
|
|
Inopy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
Inopy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along with Inopy. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
-------------------------------------------------------------------------------------
|
|
|
|
DISCLAIMER: parts of this code and comments blocks were created
|
|
with the help of ChatGPT developped by OpenAI <https://openai.com/>
|
|
Followed by human reviewing, refactoring and fine-tuning.
|
|
|
|
-------------------------------------------------------------------------------------
|
|
|
|
This code uses the values from the configuration file to construct a request payload.
|
|
|
|
The refresh function sends a POST request to the Inoreader OAuth endpoint <https://www.inoreader.com/developers/oauth> using the payload and headers to handle the refresh token process.
|
|
|
|
Then it extracts the refreshed bearer token and the new refresh token in order to use it in the replace() function of the main ino.py module.
|
|
"""
|
|
|
|
import requests
|
|
import json
|
|
|
|
# Define a function named 'refresh' that handles the token refresh logic
|
|
def refresh(config_path, endpoint, client_id, client_secret, refresh_token):
|
|
|
|
# Set the headers for the request
|
|
headers = {"Content-type": "application/x-www-form-urlencoded"}
|
|
|
|
# Prepare the payload for the request
|
|
payload = {
|
|
"client_id": client_id,
|
|
"client_secret": client_secret,
|
|
"grant_type": "refresh_token",
|
|
"refresh_token": refresh_token
|
|
}
|
|
|
|
# Send a POST request to the specified URL with the payload and headers
|
|
response = requests.post(endpoint, data=payload, headers=headers)
|
|
|
|
# Check the response status code
|
|
if response.status_code == 200:
|
|
print("Request was successful.")
|
|
else:
|
|
print("Request failed with status code:", response.status_code)
|
|
|
|
# Parse the response data as JSON
|
|
data = json.loads(response.text)
|
|
|
|
# Extract the refreshed bearer token and new refresh token from the response data
|
|
refreshed_bearer = data['access_token']
|
|
new_refresh_token = data['refresh_token']
|
|
|
|
with open(config_path, 'r+') as config_file:
|
|
# Load the JSON data from the file
|
|
config = json.load(config_file)
|
|
|
|
# Update the token value in the config data
|
|
config['oauth']['bearer'] = refreshed_bearer
|
|
config['oauth']['refresh_token'] = new_refresh_token
|
|
|
|
# Move the file pointer back to the beginning of the file
|
|
config_file.seek(0)
|
|
|
|
# Write the updated config data to the file
|
|
json.dump(config, config_file, indent=4)
|
|
config_file.truncate() |