How to deal oauth2client.tools.run_flow() when the browser unexpected terminated?

103 Views Asked by At

Following is the code I am using to get the credentials,

from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import argparse


class Drive:
    # If modifying these scopes, delete the file token.json.
    SCOPES = 'https://www.googleapis.com/auth/drive.file'

    def __init__(self):
        try:
            flags = tools.argparser.parse_args([])
        except ImportError:
            flags = None

        store = file.Storage('token.json')
        self.creds = store.get()
        if not self.creds or self.creds.invalid:
            flow = client.flow_from_clientsecrets(settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON, self.SCOPES)
            try:
                if flags:
                    try:
                        self.creds = tools.run_flow(flow, store, flags)
                    except:
                        credentials.refresh(self.http)
                        self.creds = None
            except:
                print("credentials not found")
                pass
        if self.creds == None:
            self.service = None
            pass
        else:
            self.service = build('drive', 'v3', http=self.creds.authorize(Http()))
            print("self.service",self.service)

When I am closing the browser tab without providing the credentials, it freeze the process and waits till I give the credentials. If I don't provide the credentials then everything stops working on my website.

Following is the text which display on the console till I don't provide credentials.

Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?client_id=1753xxxxxxxl4r.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file&access_type=offline&response_type=code

If your browser is on a different machine then exit and re-run this
application with the command-line parameter

  --noauth_local_webserver
0

There are 0 best solutions below