我正在将google calendar与我的django应用程序的Web应用程序集成。当我在localhost服务器上执行此操作时,其工作正常。 Google身份验证页面在客户端浏览器中打开,但是当我将该代码上传到服务器并集成Google日历时,Google身份验证页面在运行django服务器的终端中打开。
This is the page that opens for authentication in terminal
我想通过客户端Web浏览器提供此身份验证。
`
def get_credentials(request):
creds = None
# If there are no (valid) credentials available, let the user log in.
if os.path.exists('token.pickle_' + request.GET.get('bot_id')):
with open('token.pickle_' + request.GET.get('bot_id'), 'rb') as token:
creds = pickle.load(token)
print(creds)
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()
# Save the credentials for the next run
with open('token.pickle_' + request.GET.get('bot_id'), 'wb') as token:
pickle.dump(creds, token)
serializer = CalenderIntegrationSerializer(data={'bot_id': int(request.GET.get('bot_id')), 'status': True})
if serializer.is_valid():
serializer.save()
if os.path.exists('token.pickle_' + request.GET.get('bot_id')):
context = {'signin_url': creds}
return JsonResponse({'status': 200, 'data': 'Integration done!', 'is_integrated': True})
`
这是我的参考文献google calendar code python
答案 0 :(得分:0)
此代码专门用于本地开发:
https://developers.google.com/api-client-library/python/auth/installed-app
code为您提供了有关如何构造URL的提示,然后您需要将其作为临时重定向发送回用户;例如使用redirect函数。然后,您需要一个django处理程序,该处理程序接受重定向并执行该函数的后半部分。所以: