As you all can see, I'm new here. So point me out if there's any problem.
I'm having problems dealing with oauth2.0, specifically to get the access token.
I'm using this code right now:
#esse bloco serve para criar o access_token, e vai atualizar o access_token sempre, retornando ele para o principal
SCOPES = 'https://www.googleapis.com/auth/drive'
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# Esse bloco pode ser um problema para um server. É sempre preciso rodar esse bloco em um desktop para obter as chaves com autenticação.
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)
# Salva as credenciais para uma proxima vez
with open('token.json', 'w') as token:
token.write(creds.to_json())
a_file = open('token.json', "r")
tokens = json.load(a_file)
access_token = tokens['token']
This code has some problems:
- On the first time it runs, it requires manual confirmation (a internet tab opens and then you have to click some stuff).
- When the first access token expires, it doesn't work anymore (so the token is not refreshed).
Is there a way to do it automatically (without the need for a human) ? And how do I update access token using this ?
EDIT:
Since it was pointed out, here's the usual outcome of the token.json
{"token": "*******", "refresh_token": "*****", "token_uri": "********", "client_id": "*********************", "client_secret": "****************", "scopes": "https://www.googleapis.com/auth/drive", "expiry": "2021-09-17T15:45:39Z"}