Reputation: 321
I have google drive sign in feature in my react nodejs application. When ever a user clicks on sign in, I am using Google oauth2 to get code and that to get access token and refresh token using client ID and secret. I am saving these tokens and expiry in my database. Now, I want to use the refresh to get access token so that my access token never expires. Basically, I want the user to be always signed in. I am creating a post request to token to get the access token using the refresh token. This is my code:
try{
axios.post('/https://www.googleapis.com/oauth2/v3/token',{
client_id: clientId,
client_secret: secret,
refresh_token: rToken
grant_type: rToken
})
.then((response) => {
console.log("response = ", response)
}).catch((error) => {
console.log("error in getting token: = ", error)
})
} catch(err) {
console.log("error = ", err)
}
I am getting Error: connect ECONNREFUSED
I tried passing grant_type: refresh_token buy this gave reference error: refresh_token is not defined
Where am I going wrong?
Upvotes: 2
Views: 2025
Reputation: 68
First, you are using the axios.post() method to make a request to the Google OAuth2 token endpoint, but the URL you provided is incorrect. The correct URL for the token endpoint is https://www.googleapis.com/oauth2/v3/token, not /https://www.googleapis.com/oauth2/v3/token.
Here is an example of how you could correctly make a request to the Google OAuth2 token endpoint to get an access token using a refresh token:
const axios = require('axios');
const clientId = 'your-client-id';
const secret = 'your-client-secret';
const rToken = 'your-refresh-token';
try {
axios.post('https://www.googleapis.com/oauth2/v3/token', {
client_id: clientId,
client_secret: secret,
refresh_token: rToken,
grant_type: 'refresh_token'
})
.then((response) => {
console.log("response = ", response);
})
.catch((error) => {
console.log("error in getting token: = ", error);
});
} catch(err) {
console.log("error = ", err);
}
Upvotes: 4