【问题标题】:Unable to authenticate google api无法验证谷歌 api
【发布时间】:2021-11-11 05:46:06
【问题描述】:

我已经关注了这个网站上的所有内容here。我还下载了带有我的客户端 ID 和客户端密码的 .json 文件,并将其放在与我的 python 文件相同的文件夹中。当我运行我的代码时,我可以登录,但是在我继续让我的谷歌应用程序访问我的谷歌帐户后,它崩溃了。我也根本没有收到任何访问令牌。有谁知道为什么会这样?

这是 quickstart.py 文件

from __future__ import print_function
import datetime
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/calendar']


def main():
    """Shows basic usage of the Google Calendar API.
    Prints the start and name of the next 10 events on the user's calendar.
    """
    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(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=8080)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('calendar', 'v3', credentials=creds)

    # Call the Calendar API
    now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
    print('Getting the upcoming 10 events')
    events_result = service.events().list(calendarId='primary', timeMin=now,
                                        maxResults=10, singleEvents=True,
                                        orderBy='startTime').execute()
    events = events_result.get('items', [])

    if not events:
        print('No upcoming events found.')
    for event in events:
        start = event['start'].get('dateTime', event['start'].get('date'))
        print(start, event['summary'])


if __name__ == '__main__':
    main()

【问题讨论】:

    标签: google-contacts-api


    【解决方案1】:

    我曾使用 google 联系人 API 来使用 nodeJS 导出联系人。我还制作了文档,您可以在其中了解如何生成访问令牌。

    它是如何工作的?

    第一次运行示例时,它会提示您授权访问:

    我假设您已完成该步骤并接受/允许按钮。现在您已经接近生成访问令牌了。

    现在,

    1. 复制您提供的代码,将其粘贴到命令行提示符中,然后按 Enter。在此之后,文件 token.json 存储用户的访问和刷新令牌,并在授权流程第一次完成时自动创建。

    更多详情请看这里,也许它会对你有所帮助。

    google-contacts-api

    【讨论】:

    • 对不起,当你说复制我给的代码时,你的意思是 quickstart.py 吗?
    • @MattNguyen,接受/允许按钮后,您将收到一个代码。然后你必须将它粘贴到命令行prmopt中。
    • 我没有看到任何代码,到目前为止我所能做的就是登录我的 gmail,然后它把我带到我应该按继续授权我的谷歌控制台应用程序的界面访问我的数据,但随后它崩溃并显示“Safari 无法连接到服务器”
    • @MattNguyen,我不认为是浏览器的问题,但是你用其他浏览器测试过吗?
    • 是的,我也用谷歌浏览器测试过
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 2013-07-01
    • 2021-10-02
    • 2022-11-08
    • 2019-08-07
    • 2012-07-23
    • 2020-09-26
    • 2010-10-16
    相关资源
    最近更新 更多