代码之家  ›  专栏  ›  技术社区  ›  Arundeep Chohan

无法从用户获取日历api事件

  •  0
  • Arundeep Chohan  · 技术社区  · 4 年前

    因此,我目前可以使用我的社交用户帐户登录,但似乎无法访问该用户的日历api事件。 这是一个web应用程序,因此下面的代码似乎打开了另一个选项卡 .

    enter image description here

    流程:登录->主页->日历(localhost:8000/帐户/登录->localhost:8000->localhost:8000/日历)

    @login_required
    def calendar(request):
        context={}  
        results = get_user_events(request)
        context['results'] = results
        context['nmenu'] = 'calendar'
       
    
        return render(request, 'home.html', context)
    

    日历py

    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError
    import os
    def get_user_events(request):
        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())
        try:
            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.')
                return
    
            # Prints the start and name of the next 10 events
            for event in events:
                start = event['start'].get('dateTime', event['start'].get('date'))
                print(start, event['summary'])
            return events
    
        except HttpError as error:
            print('An error occurred: %s' % error)
            return []
    

    我的URI

    enter image description here

    网址。py:

    from django.conf import settings
    from django.contrib import admin
    from django.conf.urls.static import static
    from django.urls import path, include
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', include('pages.urls')),
        path('accounts/', include('django.contrib.auth.urls')),
        path('oauth/', include('social_django.urls', namespace='social'))
        
    ]
    
    if settings.DEBUG:
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Linda Lawton - DaImTo    4 年前

    重定向URI未匹配是常见的oauth错误。这是您希望将授权返回的位置。它必须在谷歌云控制台中注册

    根据您发送的错误消息 http://localhost:someport/

    这必须与您在google cloud console中为您的项目添加的其中一个完全匹配,您不希望将其列出。

    解决方案是从错误消息中获取uri并添加它。记住,它必须完全匹配,这意味着端口和尾随/。如果你的应用每次运行时都在更改端口,你需要修复它,使其脱离静态端口,以便你可以将其添加为重定向uri。

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

    推荐文章