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?
CodePudding user response:
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);
}
CodePudding user response:
To get an access token using a refresh token in Google OAuth2, you can use the refresh_token
grant type in the Google OAuth2 API. To do this using axios, you can make a POST request to the https://oauth2.googleapis.com/token
endpoint with the following parameters in the request body:
refresh_token
: The refresh token that you received when you initially authenticated the user.client_id
: The client ID for your application.client_secret
: The client secret for your application.grant_type
: The grant type, which should be set torefresh_token
.
Here's an example of how you might make this request using axios:
axios.post('https://www.googleapis.com/oauth2/v3/token', {
refresh_token: 'your-refresh-token',
client_id: 'your-client-id',
client_secret: 'your-client-secret',
grant_type: 'refresh_token',
})
.then((response) => {
// Handle the response
})
.catch((error) => {
// Handle any errors
});
This will return a response with the new access token in the access_token
field. You can then use this access token to make authorized requests to the Google API on behalf of the user.