I have a route on my backend application which should return an access token
for a code sent from the frontend:
router.get('/token', (req, res) => {
const auth = googleService.getAuth();
auth.getToken(req.query.code, (error, res2) => {
const data = { code: 200 }
if (error) {
data.code = error.code;
data.error = error.response.data;
} else {
console.log(res2);
}
res
.status(data.code)
.send(data);
})
});
I retrive auth
from googleService.getAuth():
const { google } = require('googleapis');
const keys = require('../config/keys');
var module = module.exports = {
getAuth: (token = false) => {
let auth = new google.auth.OAuth2(
keys.google.clientID,
keys.google.clientSecret,
keys.google.callbackURL
);
if (token) {
auth.credentials = {
access_token: token,
refresh_token: null
};
}
return auth;
},
youtube: google.youtube('v3')
};
In my config file, I have callbackURL
:
module.exports = {
google: {
apiKey: 'XXXXXXXXXXXXXXXX',
clientID: 'XXXXXXXXXXXXXX',
clientSecret: 'XXXXXXXXXXXXXXX',
callbackURL: 'http://localhost:3000/google/redirect'
}
}
I also set it in my console:
However, I always have the following error when calling this route:
"error": {
"error": "redirect_uri_mismatch",
"error_description": "Bad Request"
}