Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google OAuth getting new access token with refresh token

I'm trying to get my refresh_token to generate a new access_token. I'm using the request module to make the request, but It's returning an error saying something along the lines of "Could not find page".

var request = require('request');
module.exports = function(callback){
    console.log('here');
    request('https://googleapis.com/oauth2/v3/token?client_id=NotID&client_secret=Not_Secret&refresh_token=NotRefresh&grant_type=refresh_token', function (error, response, body) {
        if (!error && response.statusCode == 200) {
            callback(response)
        }   
    });
}
like image 942
Ajax jQuery Avatar asked Sep 01 '15 07:09

Ajax jQuery


2 Answers

Try this:

request.post('https://accounts.google.com/o/oauth2/token', {
  form: {
    grant_type:'refresh_token',
    refresh_token:'..',
    client_id:'..',
    client_secret:'..'
  }
}, function (err, res, body) {})
like image 176
simo Avatar answered Oct 27 '22 11:10

simo


This works..

const axios = require('axios');
const querystring = require('querystring');
const keys = require('../config/keys');

const getAccessToken = async refreshToken => {
  try {
    const accessTokenObj = await axios.post(
      'https://www.googleapis.com/oauth2/v4/token',
      querystring.stringify({
        refresh_token: refreshToken,
        client_id: keys.googleClientID,
        client_secret: keys.googleClientSecret,
        grant_type: 'refresh_token'
      })
    );
    return accessTokenObj.data.access_token;
  } catch (err) {
    console.log(err);
  }
};
like image 20
magician11 Avatar answered Oct 27 '22 12:10

magician11