I am integrating google calendar with my web application which is a django app. when i am doing it on localhost server, its working fine. Google authentication page opens in client browser, but when i am uploading that code to the server and integrating google calendar, then Google authentication page opens in terminal where i run my django server.

This is the page that opens for authentication in terminal

I want to provide this auth through client web browser.

`

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})

`

And this is my reference google calendar code python

1 Answers

0
Nino Walker On

This code is specifically for local development:

https://developers.google.com/api-client-library/python/auth/installed-app

The code gives you a hint on how to construct the URL, which you then need to send back to your user as a temporary redirect; e.g. using the redirect function. Then you need to have a django handler which accepts the redirect and executes the second half of the function. So:

  1. Split your code into two functions.
  2. Build a url, send a redirect with your endpoint as the callback
  3. Google will redirect back to your endpoint after the user completes the flow.
  4. Parse the results
  5. Execute your code.