【问题标题】:Google Sheets API v4, How to authenticate from Node.js (Docker Container)Google Sheets API v4,如何从 Node.js(Docker 容器)进行身份验证
【发布时间】:2018-01-27 05:08:03
【问题描述】:

我正在学习如何使用 Google Sheets API v.4 从工作表下载数据 --> 我的 nodeJS 服务器。我正在为我的节点应用程序使用 Docker 容器。在 Docker 中的本地主机或在线服务器上失败。它可以在 localhost 上正常工作,但不能在 Docker 容器中工作。我已在 Google API 控制台上将 IP 地址列入白名单。 (注意:我可以轻松地从这个节点服务器使用 firebase API,而不是 Google Sheets v4 API)

参考:https://developers.google.com/sheets/api/quickstart/nodejs#step_4_run_the_sample

第一次运行应用程序时,节点服务器上的命令行显示:

Authorize this app by visiting this url:  
https://accounts.google.com/o/oauth2/auth?access_type=offline&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets.readonly&response_type=code&client_id=xxx.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob

您转到该 URL,然后显示该 Google 页面:

Sign in
Please copy this code, switch to your application and paste it there.
4/xxxxxxxxxxxx

这就是问题所在。那是行不通的。我可以将4/xxx 令牌复制并粘贴到命令行中,但它失败了。没有错误信息,什么都没有。也没有功能。有没有办法从这里到达那里?我知道这在我的台式计算机上的独立节点服务器中可以正常工作,但在 docker 容器(本地主机或在线)中不行。是否有手动认证方法?

-----------编辑----------------------------------- ----------------------

我再次开始查看代码,问题是使用 docker 容器时节点 readline 失败。

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

而且 StackOveflow 上已经存在这个问题。

Unable to get standard input inside docker container

重复:

how to get docker container to read from stdin?

您需要使用 --interactive 以交互模式运行容器 或 -i:

哇...您如何在 docker-compose 部署中做到这一点?

Interactive shell using Docker Compose

哎哟。不要继续那个帖子。对我一点用都没有。请参阅下面提供的答案...

此处提供信息,以防其他人在路上遇到这种情况。

【问题讨论】:

  • 关于“手动验证方法”,您要手动检索刷新令牌吗?如果我误解了你的问题,我很抱歉。
  • 查看我的更新。此问题与how to get docker container to read from stdin? 重复。
  • 很抱歉,由于我的英语水平不佳,耽搁了您的时间。
  • 田池..没问题。你的笔记让我重新思考我的方法并再次仔细查看代码。我还在做这个..我一直无法通过终端输入内容..仍在测试。我的目的是在弄清楚发生了什么后更新这个问题。
  • 感谢您的好意。

标签: node.js docker docker-compose google-oauth google-sheets-api


【解决方案1】:

事实证明,解决方案与Interactive shell using Docker Compose提供的解决方案相去甚远

我在 docker 容器中运行节点服务器。我想使用终端在容器启动时插入一个令牌,以响应 Google 表单 API 调用,使用 Node readline 方法。

相反,我想出的解决方案是我在docker compose github issue. 中看到的一条注释的结果,对 docker compose 函数的长时间缓慢阅读让我找到了更好的解决方案。很简单:

$ docker-compose build

$ docker-compose run -p 8080:80 node 

这里有一个重要问题... node 这个词是我的服务的名称,在下面的 docker-compose.yml 文件中调用。该解决方案在我的本地主机和通过 SSH 终端的在线服务器上都运行良好。

Dockerfile:

FROM node:8
RUN mkdir -p /opt/app
# set our node environment, either development or production
ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV
# default to port 80 for node, and 5858 or 9229 for debug
ARG PORT=80
ENV PORT $PORT
EXPOSE $PORT 5858 9229
# install dependencies first, in a different location for easier app bind mounting for local development
WORKDIR /opt
COPY package.json package-lock.json* ./
RUN npm install && npm cache clean --force
ENV PATH /opt/node_modules/.bin:$PATH
# copy in our source code last, as it changes the most
WORKDIR /opt/app
COPY . /opt/app
CMD [ "node", "./bin/www" ]

docker-compose.yml

version: '3.1'
services:
  node:            <---- Name of service in the container
    build:
      context: .
      args:
        - NODE_ENV=development
    command: ../node_modules/.bin/nodemon ./bin/www --inspect=0.0.0.0:9229
    ports:
      - "80:80"
      - "5858:5858"
      - "9229:9229"
    volumes:
      - .:/opt/app
      # this is a workaround to prevent host node_modules from accidently getting mounted in container
      # in case you want to use node/npm both outside container for test/lint etc. and also inside container
      # this will overwrite the default node_modules dir in container so it won't conflict with our
      # /opt/node_modules location. Thanks to PR from @brnluiz
      - notused:/opt/app/node_modules
    environment:
      - NODE_ENV=development
    # tty: true            ## tested, not needed
    # stdin_open: true     ## tested, not needed
volumes:
    notused:

非常感谢 Bret Fisher 在node docker defaults. 上所做的工作

【讨论】:

    猜你喜欢
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2019-02-18
    • 1970-01-01
    相关资源
    最近更新 更多