'Google Gmail API returning: 401 Error - Request is missing required authentication credential but the code worked for the last few months
I have a weird problem, for a few months my web app worked just fine connection to my email account via Google API.
but today, when I tried to connect to my email via my app it return an 401 error. I'm using the same credentials in another method for sending email via nodemailer it is working fine.
maybe it is worth mentioning that I changed my email password... but I the credentials has nothing to do with my email password...
I would appreciate you help.
this is the code:
const getAuth = () => {
const CLIENT_ID = process.env.CLIENT_ID_OFFICE;
const CLIENT_SECRET = process.env.CLIENT_SECRET_OFFICE;
const REDIRECT_URI = process.env.REDIRECT_URI_OFFICE;
const REFRESH_TOKEN = process.env.REFRESH_TOKEN_OFFICE;
const oAuth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URI
);
oAuth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });
return oAuth2Client;
};
async function listMessages() {
const auth = getAuth();
const gmail = google.gmail('v1');
return new Promise((resolve, reject) => {
gmail.users.messages.list(
{
auth: auth,
userId: 'me',
q: 'is:unread has:attachment',
},
async (error, response) => {
console.log(response);
resolve(response);
}
);
});
}
and this is the respones:
{
"error": {
"code": 401,
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"errors": [
{
"message": "Login Required.",
"domain": "global",
"reason": "required",
"location": "Authorization",
"locationType": "header"
}
],
"status": "UNAUTHENTICATED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "CREDENTIALS_MISSING",
"domain": "googleapis.com",
"metadata": {
"method": "caribou.api.proto.MailboxService.ListMessages",
"service": "gmail.googleapis.com"
}
}
]
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|