我有一个从Docker容器运行的python脚本。此脚本需要Google授权过程才能与API一起使用。如何触发浏览器在容器中启动以转到授权页面并通过它?
我正在使用Docker Toolbox。 Docker版本18.03.0-CE,内部版本0520E24302 docker-compose版本1.20.1,内部版本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
答案 0 :(得分:0)
今天,我设法从Docker容器传递了Google授权。 我正在使用这种构造:
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运行容器,控制台中有一条消息,用于输入来自Google的口令以获取具有凭据的.json。而且有效。谢谢大家的帮助。