Ajax jQuery
Ajax jQuery

Reputation: 345

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)
        }   
    });
}

Upvotes: 5

Views: 1918

Answers (2)

magician11
magician11

Reputation: 4634

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);
  }
};

Upvotes: 1

simo
simo

Reputation: 15508

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) {})

Upvotes: 5

Related Questions