Browser start from Docker

Asked by At

I have a python script running from Docker container. This script requires Google authorization procedure to work with API. How to trigger browser start in container to go to authorization page and pass it?

I am working with Docker Toolbox. Docker version 18.03.0-ce, build 0520e24302 docker-compose version 1.20.1, build 5d8c71b2

$ docker run -it  -e DISPLAY=$DISPLAY docker_script
/usr/local/lib/python3.7/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.25.2) or chardet (3.0.4) doesn't match a supported version!
  RequestsDependencyWarning)
13:36:16.09: script service v1.14 b1
/usr/local/lib/python3.7/site-packages/oauth2client/_helpers.py:255: UserWarning: Cannot access /app/.credentials/google-drive-credentials.json: No such file or directory
  warnings.warn(_MISSING_FILE_MESSAGE.format(filename))

Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?client_id=285287383820-5jgb7eskc4lno8imi7sg88bbi56hilqe.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=https%3A%2F%2Fwww.g
oogleapis.com%2Fauth%2Fdrive&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

1 Answers

0
Community On

Today i have managed to pass Google authorization from Docker container. I am using this construction:

def get_credentials():

    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'sheets.googleapis.com-python-quickstart.json')
    store = Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        flags=tools.argparser.parse_args()
        flags.noauth_local_webserver=True
        credentials = tools.run_flow(flow, store, flags)
#    else: # Needed only for compatibility with Python 2.6
#        credentials = tools.run_flow(flow, store)
    print('Storing credentials to ' + credential_path)

    return credentials

SCOPES = ['https://www.googleapis.com/auth/drive']
CLIENT_SECRET_FILE = 'credentials.json'
APPLICATION_NAME = 'Tidsreg files searching script v1.14'
credentials = get_credentials()                                                
http = credentials.authorize(httplib2.Http())
drive_service_v3 = discovery.build('drive', 'v3', http=http) 

Docker runs container and there is a message in console to enter pass phrase from Google to get .json with credentials. And it works. Thanks everyone for help.