我有一个烧瓶应用程序从谷歌表中获取一些数据。当我在本地计算机上运行烧瓶应用程序时,最初它通过0auth2身份验证创建凭据并将其存储在本地。在成功完成flask本地应用程序之后,我在docker容器中创建了相同的应用程序。
场景1
容器具有在以下位置复制/添加的凭据容器创建的时间。在这种情况下,烧瓶应用程序工作没有任何问题
场景2
容器当前没有凭据文件。因此,它必须通过调用get_credentials()
函数来生成文件。此时,Web浏览器停止/继续等待来自服务器的响应(浏览器在加载阶段)
场景2是问题。希望有人可以帮助我..
我正在关注这个documentation。
Dockerfile
FROM python:2.7-slim
WORKDIR g_api
ADD . /g_api
RUN pip install -r requirements.txt
EXPOSE 8000
的搬运工-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
from flask import Flask
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from 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 from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the 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 only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
@app.route('/')
def main():
"""Shows basic usage of the Sheets API.
Creates a Sheets API service object and prints the names and majors of
students in a 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:')
for row in values:
# Print columns A and E, which correspond to indices 0 and 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
修改
容器日志(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 to quit)
* Restarting with stat
* Debugger is 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 to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 200-757-358