2

I have created project which fetch one month events of all the calendars in my calendar list. To do this, I followed google calendar api documentation Quickstart.

This is function which connect to google api.

def connect_google_api():
    creds = None
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json')
    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)
        
        with open('token.json','w') as token:
            token.write(creds.to_json())
    service = build('calendar','v3',credentials=creds)

I hosted the website using Apache and all the pages are working except google calendar. I first thought that there might be some problem while reading credentials.json so I use this to find out

data = None
with open('credentials.json') as f:
    data = json.load(f)
qw = data

Website is still in debug mode so I made some error to check local variable and I found out that data and qw variable contains credentails.json.

I think the problem is happening because of the below line

creds = flow.run_local_server(port=0)

I opened credentails.json and i found this redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]

I changed http://localhost to http://test.example.com/. I dont know what to put at the place of http://localhost. I created OAuth 2.0 Client IDs in google console and chose Application Type as Desktop App (Also tried with web application).

I want to find out what changes are required in this code so that it start working on server. On localhost everything is working fine. Please help.

Edit1

I found out that creds = flow.run_local_server(port=0) works only for localhost and I can not find any thing from documentation about replacing this so that it works on Production.

Edit2

I checked server error log and I found that

Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=somethinvf.apps.googleusercontent.com&redirect_uri=http%ivnf2Flocalhost%divnid%2F&scope=https%inviF%2Fwww.googleapis.com%2Fauth%2Fcalendar&state=something&access_type=offline, referer: https:mywebsite.com/

Why this is happening on my server console, I want this to happen on user's browser and why there is still localhost in redirect_uri?

this is my json file.

{
  "web":{
    "client_id":"something.apps.googleusercontent.com",
    "project_id":"<project id>",
    "auth_uri":"https://accounts.google.com/o/oauth2/auth",
    "token_uri":"https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
    "client_secret":"<client_secret>",
    "redirect_uris":[
      "https://mywebsite.com"
    ]
  }
}

anonymous
  • 142
  • 11
  • ** flow.run_local_server() ** launches a web server to handle the callback from the browser. Your server is running Apache. Only one server process is allowed on the same port. For public callback URLs, Google requires HTTPS. The correct strategy is to implement Google OAuth in the web server and not with ** flow.run_local_server() **. Use a plugin to make your task simple: https://dev.to/mdrhmn/django-google-authentication-using-django-allauth-18f8 – John Hanley Dec 10 '21 at 03:04
  • If any one faces same problem please refer https://stackoverflow.com/questions/68948801/python-django-why-google-oauth2-token-is-valid-after-expiration-time and https://stackoverflow.com/questions/48242761/how-do-i-use-oauth2-and-refresh-tokens-with-the-google-api – anonymous Dec 14 '21 at 08:19

0 Answers0