无法通过Google广告表API(节点JS)中的服务帐户访问写入
我有一个Google表,可以通过访问服务帐户来从Google表中阅读。以下是用于阅读的代码段。
// configure a JWT auth client
let jwtClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
['https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive']);
//authenticate request
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
} else {
console.log("Successfully connected!");
}
})
// getting data from google sheet
sheets.spreadsheets.values.get({
auth: jwtClient,
spreadsheetId: spreadsheetId,
range: range
}, function (err, response) {
if (err) {
console.log('The API returned an error: ' + err);
} else {
console.log('Users list from Google Sheets:', response.data.values);
}
});
这可以很好地工作,我正在获取数据。但是,当我想写入Google表时,它会出现错误。 的片段
// writing to google sheets
let values = [
[
'abc','b.ed'
],
];
const requestBody = {
values,
};
sheets.spreadsheets.values.update({
spreadsheetId,
range:"Sheet1!C3",
valueInputOption,
requestBody,
}, (err, result) => {
if (err) {
// Handle error
console.log(err);
} else {
console.log('%d cells updated.', result.updatedCells);
}
});
写错误
gaxioserror:登录所需。 在gaxios._request(/home/drapl表验证(节点JS +服务帐户)/node_modules/gaxios/build/src/gaxios.js:129:23) 在processTickSandRepoxtions(节点:内部/process/task_queues:96:5){ 错误:[ { 消息:“登录需要。”, 域:“全局”, 原因:“必需”, 位置:“授权”, 位置类型:“标题” } ] }
I have a google sheet and I am able to read from google sheet by giving access to service account. Below is the snippet of code for reading.
// configure a JWT auth client
let jwtClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
['https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive']);
//authenticate request
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
} else {
console.log("Successfully connected!");
}
})
// getting data from google sheet
sheets.spreadsheets.values.get({
auth: jwtClient,
spreadsheetId: spreadsheetId,
range: range
}, function (err, response) {
if (err) {
console.log('The API returned an error: ' + err);
} else {
console.log('Users list from Google Sheets:', response.data.values);
}
});
this works perfectly, and i am getting the data. But when I want to write to google sheet, it gives an error. Snippet of writing
// writing to google sheets
let values = [
[
'abc','b.ed'
],
];
const requestBody = {
values,
};
sheets.spreadsheets.values.update({
spreadsheetId,
range:"Sheet1!C3",
valueInputOption,
requestBody,
}, (err, result) => {
if (err) {
// Handle error
console.log(err);
} else {
console.log('%d cells updated.', result.updatedCells);
}
});
The error I am getting
GaxiosError: Login Required.
at Gaxios._request (/home/spread sheet auth (node js + service account)/node_modules/gaxios/build/src/gaxios.js:129:23)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
errors: [
{
message: 'Login Required.',
domain: 'global',
reason: 'required',
location: 'Authorization',
locationType: 'header'
}
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们查看工作示例,您
注意到了如何添加AUTH并将其设置为JWTClient?这将授权权限添加到您的电话中。
现在,让我们查看
您尚未添加auth的更新陈述,这意味着您正在尝试在不经过身份验证的情况下预先构造此操作。更新对用户的私人数据行为,您需要权限以便能够调用此方法。
因此,通过该逻辑,以下内容应解决您的错误
Lets look at the working example you have
Notice how you have added auth and set it to your jwtClient? This adds the authorization permission to your call.
Now lets look at your update statment
You have not added auth, which means you are trying to preform this action without being authenticated. Update acts upon a users private data you need permission to be able to call this method.
So by that logic the following should fix your error