Not returning refresh token from auth code
Asked Answered
I

1

6

I'm using googleapis package from node to get refresh token and access token from auth code passed from front-end but every time I get the following error.

  {      
      error: 'redirect_uri_mismatch',
      error_description: 'Bad Request' 
  }

I know this error comes up when we mismatch URL passed as a callback URL in the console. https://console.cloud.google.com/apis/credentials

but I've already set up the correct URL in the console. still not sure what's the problem with the code.

Using /auth to pass the token from front-end to node-server.

const {
    google
} = require("googleapis");
const OAuth2 = google.auth.OAuth2;
var bodyParser = require('body-parser')

const express = require('express');
const app = express();
app.use(bodyParser.json());

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});


app.use('/tokenCallback', (req, res) => {
    console.log(req);
    res.send('An alligator approaches!');
});

app.post('/auth', (req, res) => {
    runProcess(req.body.auth);
    res.send('An alligator approaches!');
});

app.listen(4300);

function runProcess(code) {

    const oauth2client = new OAuth2(
        "210347756664-xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
        "57ZU6JuQ7oQ7SvSFtki5atxx", // Client Secret
        "http://localhost:4300/tokenCallback",
    );

    oauth2client.getToken(code, (e) => {
        console.log(e);
    });

    oauth2client.on('tokens', (tokens) => {
        if (tokens.refresh_token) {
            console.log("refresh token", tokens);
        }
    });

}

Any help from anyone will be greatly appreciated.

Ihab answered 9/10, 2018 at 18:55 Comment(1)
E
0

The redirect_uri_mismatch means that your application isn't sending a valid redirect URI, or it is not registered on Google Cloud Console.

Make sure that in Console -> APIs & Services -> Credentials -> OAuth client IDs -> (your key) you have added the http://localhost:4300/tokenCallback URI.

Effulgent answered 26/10, 2018 at 4:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.