Как получить токен доступа с помощью OAuth2WebServerFlow в python? - PullRequest
0 голосов
/ 03 апреля 2020

Я попытался получить токен доступа и обновить sh токен с помощью OAuth2WebServerFlow в python. но он выдает ошибку я пытался передать код в step2_exchange method.it выдает эту ошибку ('oauth2client.client.FlowExchangeError: invalid_grantMalformed auth code.') как решить эту проблему.

1 Ответ

0 голосов
/ 25 апреля 2020

Выполните следующие действия: (1) go до https://console.cloud.google.com/ (2) выберите свой аккаунт (3) Go для «меню навигации» в верхнем правом меню бургера (4) Выберите сервисы API и выберите «учетные данные» (5) создайте учетные данные и загрузите файл json в рабочий каталог (6) fini sh oauth также на экране клиента, а затем скопируйте и вставьте следующий python код

    import pickle
    from googleapiclient.discovery import build
    from google_auth_oauthlib.flow import InstalledAppFlow
    from google.auth.transport.requests import Request

    # If modifying these scopes, delete the file token.pickle.
    SCOPES = ['https://www.googleapis.com/auth/drive.file']
    CLIENT_SECRET_FILE = 'e:\\Python Programs\\credentials.json'

    creds = None
    # The file token.pickle 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.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(CLIENT_SECRET_FILE, 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)

    drive_service = build('drive', 'v3', credentials=creds)

Это способ доступа к вашему токену через flow.run_local_server

...