Search code examples
oauth-2.0google-drive-api

Why oauth2 url is not opened on my client side?


I have a problem about my google drive oauth2.

I have this code :

def getService(creds):

    service = build('drive', 'v3', credentials=creds)
    return service


def getCredentials():

    # If modifying these scopes, delete the file token.json.
    SCOPES = ['https://www.googleapis.com/auth/drive']
    """Shows basic usage of the Drive v3 API.
        Prints the names and ids of the first 10 files the user has access to.
        """
    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(
                'code_clientXXX.json',
                SCOPES)

            creds = flow.run_local_server(port=0)
            print(creds)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())
    return creds

When I run in localhost, i works very well, this url is automatically opened on my browser: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=XXXX&[..............]

But, when I deploy my front (angular app) and my back (django app) on a distant server, using docker. There is a problem.

1 - If my token.json is already generated : It works very well.

2 - If my token.json is not already generated : nothing is asked on client side.

If I check django logs :

sudo docker logs c289011c7be6
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=XXX-XXX.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A46523%2F&sc
ope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=XXX&access_type=offline

1- Why this url is not opened on my client side

2- why redirect is localhost and not my ip adress?

Thansk a lot


Solution

  •  flow = InstalledAppFlow.from_client_secrets_file(
                'code_clientXXX.json',
                SCOPES)
    

    Oauth.md

    The google_auth_oauthlib.flow.InstalledAppFlow class is used for installed applications. This flow is useful for local development or applications that are installed on a desktop operating system. See OAuth 2.0 for Installed Applications.

    Your code is designed to run as an installed application this means that the consent screen is going to open up on the machine the code is running on. If you try to deeply this into docker is going to try and open a browser window in the docker container which is not going to work there is no one there to see it.