python - docker: error with google-sheet-api minimal example -
i have flask app fetches data google sheet. when run flask app on local machine, it's creating credentials 0auth2 authentication , stores on local. after successful completion of flask local app, made same app inside docker container.
scenario 1
the container has credentials copied/added @ time of container creation. on case flask app working without issues
scenario 2
the container has no credentials file. so, has generate file calling get_credentials()
function. @ point, web browser stucked/keep on waiting response server ( browser @ stage of loading )
scenario 2 problem. hope can me ..
i'm following documentation.
dockerfile
from python:2.7-slim workdir g_api add . /g_api run pip install -r requirements.txt expose 8000
docker-compose.yml
version: "3" services: web: build: context: . dockerfile: dockerfile command: "python g_api.py" ports: - "8000:8000" networks: - webnet volumes: - .:/g_api networks: webnet:
requirements.txt
flask==0.12.2 google-api-python-client==1.6.2
g_api.py
from __future__ import print_function flask import flask import httplib2 import os apiclient import discovery oauth2client import client oauth2client import tools oauth2client.file import storage app = flask(__name__) try: import argparse flags = argparse.argumentparser(parents=[tools.argparser]).parse_args() except importerror: flags = none scopes = 'https://www.googleapis.com/auth/spreadsheets.readonly' client_secret_file = 'client_secret.json' application_name = 'google sheets api python quickstart' def get_credentials(): """gets valid user credentials storage. if nothing has been stored, or if stored credentials invalid, oauth2 flow completed obtain new credentials. returns: credentials, obtained credential. """ credential_dir = os.path.join(os.getcwd(), '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 if flags: credentials = tools.run_flow(flow, store, flags) else: # needed compatibility python 2.6 credentials = tools.run(flow, store) print('storing credentials ' + credential_path) return credentials @app.route('/') def main(): """shows basic usage of sheets api. creates sheets api service object , prints names , majors of students in sample spreadsheet: https://docs.google.com/spreadsheets/d/1bximvs0xra5nfmdkvbdbzjgmuuqptlbs74ogve2upms/edit """ credentials = get_credentials() http = credentials.authorize(httplib2.http()) discoveryurl = ('https://sheets.googleapis.com/$discovery/rest?' 'version=v4') service = discovery.build('sheets', 'v4', http=http, discoveryserviceurl=discoveryurl) spreadsheetid = '1bximvs0xra5nfmdkvbdbzjgmuuqptlbs74ogve2upms' rangename = 'class data!a2:e' result = service.spreadsheets().values().get( spreadsheetid=spreadsheetid, range=rangename).execute() values = result.get('values', []) if not values: return "not success" # print('no data found.') else: print('name, major:') row in values: # print columns , e, correspond indices 0 , 4. print('%s, %s' % (row[0], row[4])) return "success" if __name__ == '__main__': app.run(host='0.0.0.0', port=int(8000), debug=true)
directory_sructure
.g_api ├── client_secret.json ├── credentials ├── docker-compose.yml ├── dockerfile ├── g_api.py └── requirements.txt
edit
the container logs ( docker logs cont_id )
warning: error loading config file:/home/jpg/.docker/config.json - stat /home/jpg/.docker/config.json: permission denied * running on http://0.0.0.0:8000/ (press ctrl+c quit) * restarting stat * debugger active! * debugger pin: 200-757-358 /usr/local/lib/python2.7/site-packages/oauth2client/_helpers.py:255: userwarning: cannot access /g_api/credentials/sheets.googleapis.com-python-quickstart.json: no such file or directory warnings.warn(_missing_file_message.format(filename)) * running on http://0.0.0.0:8000/ (press ctrl+c quit) * restarting stat * debugger active! * debugger pin: 200-757-358
Comments
Post a Comment