I am using google/genai sdk and keep getting the following error.
GaxiosError: invalid_grant
at Gaxios._request (C:projectstideelectronnode_modulesgaxiosbuildsrcgaxios.js:142:23)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async UserRefreshClient.refreshTokenNoCache (C:projectstideelectronnode_modulesgoogle-auth-librarybuildsrcauthoauth2client.js:212:19)
at async UserRefreshClient.getRequestMetadataAsync (C:projectstideelectronnode_modulesgoogle-auth-librarybuildsrcauthoauth2client.js:333:17)
at async UserRefreshClient.getRequestHeaders (C:projectstideelectronnode_modulesgoogle-auth-librarybuildsrcauthoauth2client.js:296:26)
at async NodeAuth.addGoogleAuthHeaders (C:[email protected]:13617:29)
at async ApiClient.getHeadersInternal (C:[email protected]:10982:9)
at async ApiClient.includeExtraHttpOptionsToRequestInit (C:[email protected]:10856:31)
at async ApiClient.request (C:[email protected]:10805:23)
at async Models.generateContent (C:[email protected]:12024:24) {
config: {
retry: true,
retryConfig: {
httpMethodsToRetry: [Array],
currentRetryAttempt: 0,
retry: 3,
noResponseRetries: 2,
retryDelayMultiplier: 2,
timeOfFirstRequest: 1748569979920,
totalTimeout: 9007199254740991,
maxRetryDelay: 9007199254740991,
statusCodesToRetry: [Array]
},
method: 'POST',
url: 'https://oauth2.googleapis.com/token',
data: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'google-api-nodejs-client/9.15.1',
'x-goog-api-client': 'gl-node/22.15.1'
},
paramsSerializer: [Function: paramsSerializer],
body: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
validateStatus: [Function: validateStatus],
responseType: 'unknown',
errorRedactor: [Function: defaultErrorRedactor]
},
response: {
config: {
retry: true,
retryConfig: [Object],
method: 'POST',
url: 'https://oauth2.googleapis.com/token',
data: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
headers: [Object],
paramsSerializer: [Function: paramsSerializer],
body: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
validateStatus: [Function: validateStatus],
responseType: 'unknown',
errorRedactor: [Function: defaultErrorRedactor]
},
data: { error: 'invalid_grant', error_description: 'Bad Request' },
headers: {
'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
'cache-control': 'no-cache, no-store, max-age=0, must-revalidate',
'content-encoding': 'gzip',
'content-type': 'application/json; charset=utf-8',
date: 'Fri, 30 May 2025 01:52:59 GMT',
expires: 'Mon, 01 Jan 1990 00:00:00 GMT',
pragma: 'no-cache',
server: 'scaffolding on HTTPServer2',
'transfer-encoding': 'chunked',
vary: 'Origin, X-Origin, Referer',
'x-content-type-options': 'nosniff',
'x-frame-options': 'SAMEORIGIN',
'x-xss-protection': '0'
},
status: 400,
statusText: 'Bad Request',
request: { responseURL: 'https://oauth2.googleapis.com/token' }
},
error: undefined,
status: 400,
[Symbol(gaxios-gaxios-error)]: '6.7.1'
}
Code that calls it:
const genAI = new GoogleGenAI((apiKey = apiKey));
const response = await genAI.models.generateContent({
model: "gemini-2.0-flash",
contents: "test prompt",
});
I know the key works because I tested it in test.js file
import { GoogleGenAI } from "@google/genai";
const GEMINI_API_KEY = "GEMINI_KEY";
const ai = new GoogleGenAI({ apiKey: GEMINI_API_KEY });
async function main() {
const response = await ai.models.generateContent({
model: "gemini-2.0-flash",
contents: "Why is the sky blue?",
});
console.log(response.text);
}
main();
Any idea why this might be happening?
I run the code using npm start which is defined in package.json as:
"scripts": {
"start": "electron .",
"build": "electron-builder",
"test": "echo "Error: no test specified" && exit 1"
}