所以我目前可以使用我的社交用戶帳戶登錄,但似乎無法訪問該用戶的日歷 API 事件。它是一個網路應用程式,因此以下代碼似乎打開了另一個選項卡。

流程:登錄->主頁->日歷(localhost:8000/accounts/login-> 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 []
我的尤里斯

網址.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)
uj5u.com熱心網友回復:
我認為您可能會發現這很有用https://simplyscheduleappointments.com/guides/400-redirect_uri_mismatch-error/
uj5u.com熱心網友回復:
重定向 URI 未匹配是常見的 oauth 錯誤。這是您希望將授權回傳到的位置。必須在谷歌云控制臺注冊
根據您發送的錯誤訊息 http://localhost:someport/
這必須與您在谷歌云控制臺中為您的專案添加的專案之一完全匹配,您似乎沒有列出該專案。
解決方案是從錯誤訊息中獲取uri并添加它。請記住,它必須完全匹配,這意味著埠和您的尾隨 /。如果您的應用程式在每次運行時都更改埠,則需要修復它,使其從靜態埠運行,以便您可以將其添加為重定向 uri。
如果您不知道如何解決此問題,此視頻將向您展示如何解決。Google OAuth2:如何修復 redirect_uri_mismatch 錯誤。第 2 部分服務器端 Web 應用程式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/422725.html
標籤:
上一篇:在django中找出月份的差異
下一篇:我無法發布資料-Django
