简体   繁体   中英

Google Sheets API v4, How to authenticate from Node.js (Docker Container)

I'm learning how to use Google Sheets API v.4 to download data from a sheet --> my nodeJS server. I'm using Docker containers for my node app. Fails on either localhost or online at server in Docker. It will work fine on localhost, but not in a Docker container. I've whitelisted the IP address at the Google API console. (note: I'm easily able to use firebase API from this node server, not the Google Sheets v4 API)

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

First time you run the app, the command line on the node server displays:

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

You go to that URL, and that Google page displays:

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

And here's the rub. No way will that work. I can copy and paste the 4/xxx token into the command line, but it's a fail. No error message, no nothing. No function either. Is there a way to get there from here? I know this works fine in a stand alone Node server on my desktop computer , but not in a docker container (either localhost or online). Is there a manual method for the authentication?

-----------Edit---------------------------------------------------------

I started looking at the code again, and the issue is a fail on node readline while using a docker container.

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

And that issue already exists here on StackOveflow.

Unable to get standard input inside docker container

duplicate of:

how to get docker container to read from stdin?

You need to run the container in interactive mode with --interactive or -i:

Whoa... and how do you do that in a docker-compose deployment?

Interactive shell using Docker Compose

Ouch. No go on that posting. Didn't work at all for me.. See the answer provided below...

Info provided here in case anybody else hits this bump in the road.

So it turns out the solution was nowhere near that provided by Interactive shell using Docker Compose

I'm running a node server in a docker container. I wanted to use the terminal to insert a token upon container startup in response to Google sheet API call, using the Node readline method.

Instead the solution I came up with was the result of a note I saw in a docker compose github issue. A long slow read of docker compose functions got me to a better solution. It was as simple as:

$ docker-compose build

$ docker-compose run -p 8080:80 node 

One important issue here... the word node is the name of my service as called out in the docker-compose.yml file below. This solution worked fine on both my localhost and at an online server via SSH terminal.

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:

Many thanks to Bret Fisher for his work on node docker defaults.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM