Google Sheets API v4,如何从 Node.js(Docker 容器)进行身份验证
Google Sheets API v4, How to authenticate from Node.js (Docker Container)
我正在学习如何使用 Google Sheets API v.4 从 sheet --> 我的 nodeJS 服务器下载数据。我正在为我的节点应用程序使用 Docker 容器。 Docker 中的本地主机或服务器联机失败。它可以在本地主机上正常工作,但不能在 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 容器时节点读取线失败。
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
StackOveflow 上已经存在这个问题。
Unable to get standard input inside docker container
重复:
You need to run the container in interactive mode with --interactive
or -i:
哇...你是如何在 docker-compose 部署中做到这一点的?
Interactive shell using Docker Compose
哎呀。不要继续那个帖子。对我来说根本没用。请参阅下面提供的答案...
此处提供的信息以防其他人遇到这个颠簸。
所以事实证明解决方案远不及 Interactive shell using Docker Compose
提供的解决方案
我是 运行 docker 容器中的节点服务器。我想使用终端在容器启动时插入令牌以响应 Google sheet API 调用,使用 Node readline 方法。
相反,我想出的解决方案是我在 docker compose github issue. 中看到的注释的结果 docker 撰写函数的长时间缓慢阅读让我找到了更好的解决方案。很简单:
$ docker-compose build
$ docker-compose run -p 8080:80 node
这里有一个重要问题... node
一词是我在下面的 docker-compose.yml 文件中调用的服务的名称。该解决方案在我的本地主机和通过 SSH 终端的在线服务器上都运行良好。
Docker 文件:
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.
上所做的工作
我正在学习如何使用 Google Sheets API v.4 从 sheet --> 我的 nodeJS 服务器下载数据。我正在为我的节点应用程序使用 Docker 容器。 Docker 中的本地主机或服务器联机失败。它可以在本地主机上正常工作,但不能在 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 容器时节点读取线失败。
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
StackOveflow 上已经存在这个问题。
Unable to get standard input inside docker container
重复:
You need to run the container in interactive mode with --interactive or -i:
哇...你是如何在 docker-compose 部署中做到这一点的?
Interactive shell using Docker Compose
哎呀。不要继续那个帖子。对我来说根本没用。请参阅下面提供的答案...
此处提供的信息以防其他人遇到这个颠簸。
所以事实证明解决方案远不及 Interactive shell using Docker Compose
提供的解决方案我是 运行 docker 容器中的节点服务器。我想使用终端在容器启动时插入令牌以响应 Google sheet API 调用,使用 Node readline 方法。
相反,我想出的解决方案是我在 docker compose github issue. 中看到的注释的结果 docker 撰写函数的长时间缓慢阅读让我找到了更好的解决方案。很简单:
$ docker-compose build
$ docker-compose run -p 8080:80 node
这里有一个重要问题... node
一词是我在下面的 docker-compose.yml 文件中调用的服务的名称。该解决方案在我的本地主机和通过 SSH 终端的在线服务器上都运行良好。
Docker 文件:
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.
上所做的工作