Учетные данные Google не получают токен обновления?Python

Программы на Python
Ответить
Anonymous
 Учетные данные Google не получают токен обновления?

Сообщение Anonymous »

Я пытался получить доступ к Gmail через клиент Google oauth2. Однако у меня не получается заставить это работать:

Код: Выделить всё

def readEmails():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# 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(
# your creds file here. Please create json file as here https://cloud.google.com/docs/authentication/getting-started
credential_path, SCOPES)
creds = flow.run_local_server(port=4000)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
try:
# Call the Gmail API
service = build('gmail', 'v1', credentials=creds)
results = service.users().messages().list(userId='me', labelIds=['INBOX'], q="is:unread").execute()
messages = results.get('messages',[]);
if not messages:
print('No new messages.')
else:
print('would read')
return
except Exception as error:
print(f'An error occurred: {error}')
При первом запуске он работает нормально, получая всю необходимую информацию. Однако при втором запуске, когда он читает файл токена, выдается ошибка:

Код: Выделить всё

ValueError: Authorized user info was not in the expected format, missing fields refresh_token.
если я просматриваю json, я действительно не вижу никакой части токена обновления в json.
Я замечаю это уже происходит в «from_authorized_user_file»

Подробнее здесь: https://stackoverflow.com/questions/793 ... resh-token
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»