title: Google oauth2 with node js
body: <p>I have a Vuejs application and I am trying to add google authentication. I use this plugin <a href="https://www.npmjs.com/package/vue-google-oauth2" rel="nofollow noreferrer">https://www.npmjs.com/package/vue-google-oauth2</a> to generate the authorization code on the front end and then send it to my back-end so it can get the user credentials.</p>
  
  <p>Here is the code from the front end:</p>
  
  <pre><code>&lt;script&gt;
  import axios from "axios";
  export default {
    methods: {
      googleAuth() {
        this.$gAuth
          .getAuthCode()
          .then(authCode =&gt; {
            //on success
            axios.post('my-back-end', {code:authCode, redirect_uri: 'postmessage'}).then(res=&gt;{
                console.log(res);
            });
          })
          .catch(error =&gt; {
            //on fail do something
          });
      }
    }
  };
  &lt;/script&gt;
  </code></pre>
  
  <p>I get the authorization code successfully until here and i send it to my back end which is written with node.js based on the official google docs. <a href="https://github.com/googleapis/google-api-nodejs-client#oauth2-client" rel="nofollow noreferrer">https://github.com/googleapis/google-api-nodejs-client#oauth2-client</a></p>
  
  <p>I have this route :</p>
  
  <pre><code>router.post('/google', googleController.getGoogleAccountFromCode);
  </code></pre>
  
  <p>And this controller:</p>
  
  <pre><code>const {google} = require('googleapis');
  
  const oauth2Client = new google.auth.OAuth2(
      process.env.GOOGLE_CLIENT_ID,
      process.env.GOOGLE_CLIENT_SECRET,
      process.env.GOOGLE_REDIRECT_URL
  );
  
  exports.getGoogleAccountFromCode = (req, res, next) =&gt; {
     const code = req.body.code;
     const data = oauth2Client.getToken(code)
      .then(res =&gt; {
          console.log(res);
      })
      .catch(err=&gt;{
          console.log(err);
      });
  };
  </code></pre>
  
  <p>And I get this error:</p>
  
  <blockquote>
    <p>{ Error: invalid_request
        at Gaxios.request (/home/monkeydkon/Desktop/tabata-rest/node_modules/gaxios/build/src/gaxios.js:70:23)
        at process._tickCallback (internal/process/next_tick.js:68:7)   response:    { config:
          { method: 'POST',
            url: '<a href="https://oauth2.googleapis.com/token" rel="nofollow noreferrer">https://oauth2.googleapis.com/token</a>',
            data:
             'code=4%2FlgGDaumBZrCEo1GraV2csRBqKMOQFM7IKhBUP3tJVf2NSPS2nBqbdc9mDqIuaM847ZIPy6mZ4MGHLD9fR2a3A_Q&amp;client_id=918021882776-fu8hr3q5ld81t1dlv1pd8en7ht8hu3t6.apps.googleusercontent.com&amp;client_secret=LBCef5d7K48XGB7TEEDF7SBK&amp;redirect_uri=%2Fauth%2Fgoogle%2Fredirect&amp;grant_type=authorization_code&amp;code_verifier=',
            headers: [Object],
            params: [Object: null prototype] {},
            paramsSerializer: [Function: paramsSerializer],
            body:
             'code=4%2FlgGDaumBZrCEo1GraV2csRBqKMOQFM7IKhBUP3tJVf2NSPS2nBqbdc9mDqIuaM847ZIPy6mZ4MGHLD9fR2a3A_Q&amp;client_id=918021882776-fu8hr3q5ld81t1dlv1pd8en7ht8hu3t6.apps.googleusercontent.com&amp;client_secret=LBCef5d7K48XGB7TEEDF7SBK&amp;redirect_uri=%2Fauth%2Fgoogle%2Fredirect&amp;grant_type=authorization_code&amp;code_verifier=',
            validateStatus: [Function: validateStatus],
            responseType: 'json' },
         data:
          { error: 'invalid_request',
            error_description:
             'Invalid parameter value for redirect_uri: Missing scheme: /auth/google/redirect' },
         headers:
          { 'alt-svc': 'quic=":443"; ma=2592000; v="46,43,39"',
            'cache-control': 'private',
            connection: 'close',
            'content-encoding': 'gzip',
            'content-type': 'application/json; charset=utf-8',
            date: 'Wed, 31 Jul 2019 20:47:01 GMT',
            server: 'scaffolding on HTTPServer2',
            'transfer-encoding': 'chunked',
            vary: 'Origin, X-Origin, Referer',
            'x-content-type-options': 'nosniff',
            'x-frame-options': 'SAMEORIGIN',
            'x-xss-protection': '0' },
         status: 400,
         statusText: 'Bad Request' },   config:    { method: 'POST',
         url: '<a href="https://oauth2.googleapis.com/token" rel="nofollow noreferrer">https://oauth2.googleapis.com/token</a>',
         data:
          'code=4%2FlgGDaumBZrCEo1GraV2csRBqKMOQFM7IKhBUP3tJVf2NSPS2nBqbdc9mDqIuaM847ZIPy6mZ4MGHLD9fR2a3A_Q&amp;client_id=918021882776-fu8hr3q5ld81t1dlv1pd8en7ht8hu3t6.apps.googleusercontent.com&amp;client_secret=LBCef5d7K48XGB7TEEDF7SBK&amp;redirect_uri=%2Fauth%2Fgoogle%2Fredirect&amp;grant_type=authorization_code&amp;code_verifier=',
         headers:
          { 'Content-Type': 'application/x-www-form-urlencoded',
            'User-Agent': 'google-api-nodejs-client/4.2.6',
            Accept: 'application/json' },
         params: [Object: null prototype] {},
         paramsSerializer: [Function: paramsSerializer],
         body:
          'code=4%2FlgGDaumBZrCEo1GraV2csRBqKMOQFM7IKhBUP3tJVf2NSPS2nBqbdc9mDqIuaM847ZIPy6mZ4MGHLD9fR2a3A_Q&amp;client_id=918021882776-fu8hr3q5ld81t1dlv1pd8en7ht8hu3t6.apps.googleusercontent.com&amp;client_secret=LBCef5d7K48XGB7TEEDF7SBK&amp;redirect_uri=%2Fauth%2Fgoogle%2Fredirect&amp;grant_type=authorization_code&amp;code_verifier=',
         validateStatus: [Function: validateStatus],
         responseType: 'json' },   code: '400' }</p>
  </blockquote>
  
  <p>I really cannot understand google docs. There is not proper support for oauth2. I really need some help here.
  Thanks</p>