首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用访问令牌获取Google-Calendar事件

如何使用访问令牌获取Google-Calendar事件
EN

Stack Overflow用户
提问于 2021-10-28 04:35:10
回答 1查看 736关注 0票数 2

我已经建立了一个django应用程序,其中包括谷歌Oauth2.0登录。我想得到每个用户的谷歌日历事件,当他们使用Oauth2.0登录时,我写了以下代码。我将访问令牌保存到UserAuth表中并获取它,然后使用它来获取谷歌日历。

代码语言:javascript
复制
def get_events_server(request):
    user = User.objects.get(username=request.user)
    creds = UserAuth.objects.get(user=user).google_id_token

    credentials = AccessTokenCredentials(creds, "")
    http = httplib2.Http()
    http = credentials.authorize(http)

    service = build('calendar', 'v3', http=http)
    return service

当我运行代码时,发生了以下错误。

代码语言:javascript
复制
HttpError at /calendar/
<HttpError 403 when requesting https://www.googleapis.com/calendar/v3/calendars/primary/events?timeMin=2021-10-28T04%3A33%3A08.956703Z&timeMax=2021-11-04T04%3A33%3A08.956712Z&singleEvents=true&timeZone=GMT%2B9%3A00&orderBy=startTime&alt=json returned "Request had insufficient authentication scopes.". Details: "[{'message': 'Insufficient Permission', 'domain': 'global', 'reason': 'insufficientPermissions'}]">

有没有跳过这个问题的解决方案?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-28 07:54:44

您在这里有点困惑,让我们先来看看身份验证和授权之间的区别。

身份验证或开放式Id连接是让用户登录到他们的google帐户,您将获得id令牌,并且您能够访问他们的个人资料信息,因为用户登录了。您正在验证计算机背后的用户是否拥有该帐户。在您的代码中,可以看到您正在使用的id_token Open id connect来验证用户。

代码语言:javascript
复制
creds = UserAuth.objects.get(user=user).google_id_token

为了访问用户的私有数据,您的应用程序需要被授权访问该数据。授权由作用域或您需要的访问作用域定义。为了使用google日历api,您需要一个访问令牌,其范围允许您访问用户的google日历事件

您应该看一下Python quickstart for google calendar,它将向您展示如何使用Oauth2让您的应用程序请求用户授权来访问他们的google日历数据。

代码语言:javascript
复制
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=0)
        # 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', [])

来自评论

您的链接同意屏幕请求返回错误。这是重定向uri未匹配错误,也是设置oauth2时最常见的错误之一。

如果你检查这个错误,它会告诉你这个url redirect_uri: http://localhost:61668/有问题,你正从这个url发送你的请求。这意味着您需要转到google云控制台,并将该重定向uri添加到您的已接受重定向uri列表中。请记住,它必须完全匹配,因此必须包括端口号和尾部斜杠。

这些是您需要添加http://localhost:61668/的当前重定向uris

尝试设置

代码语言:javascript
复制
flow.run_local_server(port=0)

代码语言:javascript
复制
flow.run_local_server(port=8000)

然后添加

代码语言:javascript
复制
http://localhost:8000/

作为您的重定向uri。

如果您不知道本视频将如何向您展示如何修复它。Google OAuth2: How the fix redirect_uri_mismatch error. Part 2 server sided web applications.

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69748355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档