Unauthorized for only clear requests Google 张 API
Unauthorized for only clear requests Google Sheets API
我在 node.js 应用程序中使用 Google Sheets API v4。我正在使用范围设置为“http://www.googleapis.com/auth/spreadsheets”的 JWT 客户端进行身份验证。我发现我可以成功更新 sheet 或执行追加操作,但在尝试清除 sheet.
时出现 401 Unauthorized 错误
在我的开发环境中,我使用的服务用户不属于我所属的组织。这会影响哪些呼叫被授权吗?查看有关在组织级别设置共享权限的文档,控件似乎没有那么细粒度。
auth(scopes) {
const key = process.env.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY;
const email = process.env.GOOGLE_SERVICE_ACCOUNT_CLIENT_EMAIL;
const jwtClient = new google.auth.JWT(email, null, key, scopes);
jwtClient.authorize((err) => {
if (err) {
console.log(err);
}
});
return jwtClient;
}
...
async updateSheet(jwtClient, spreadsheet_id, sheet_name, data) {
const write = util.promisify(sheets.spreadsheets.values.update);
const body = {
majorDimension: 'COLUMNS',
values: data
};
const request = {
spreadsheetId: spreadsheet_id,
range: sheet_name,
valueInputOption: 'user_entered',
resource: body,
auth: jwtClient,
};
const response = await write(request);
return response;
},
async clearSheet(jwtClient, spreadsheet_id, sheet_name) {
const clear = util.promisify(sheets.spreadsheets.values.clear);
const request = {
spreadsheetId: spreadsheet_id,
range: sheet_name
};
const response = await clear(request);
return response;
}
使用相同的jwt客户端,spreadsheetid,sheetname,第一次调用成功,第二次调用失败
await google.updateSheet(google_jwtClient, spreadsheet_id, sheet_name, data);
await google.clearSheet(google_jwtClient, spreadsheet_id, sheet_name);
{ 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.',
domain: 'global',
reason: 'unauthorized' }
'Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential.
表示您没有随请求发送凭据。
您没有将 jwtClient 应用于请求。
async clearSheet(jwtClient, spreadsheet_id, sheet_name) {
const clear = util.promisify(sheets.spreadsheets.values.clear);
const request = {
spreadsheetId: spreadsheet_id,
auth: jwtClient,
range: sheet_name
};
const response = await clear(request);
return response;
}
我在 node.js 应用程序中使用 Google Sheets API v4。我正在使用范围设置为“http://www.googleapis.com/auth/spreadsheets”的 JWT 客户端进行身份验证。我发现我可以成功更新 sheet 或执行追加操作,但在尝试清除 sheet.
时出现 401 Unauthorized 错误在我的开发环境中,我使用的服务用户不属于我所属的组织。这会影响哪些呼叫被授权吗?查看有关在组织级别设置共享权限的文档,控件似乎没有那么细粒度。
auth(scopes) {
const key = process.env.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY;
const email = process.env.GOOGLE_SERVICE_ACCOUNT_CLIENT_EMAIL;
const jwtClient = new google.auth.JWT(email, null, key, scopes);
jwtClient.authorize((err) => {
if (err) {
console.log(err);
}
});
return jwtClient;
}
...
async updateSheet(jwtClient, spreadsheet_id, sheet_name, data) {
const write = util.promisify(sheets.spreadsheets.values.update);
const body = {
majorDimension: 'COLUMNS',
values: data
};
const request = {
spreadsheetId: spreadsheet_id,
range: sheet_name,
valueInputOption: 'user_entered',
resource: body,
auth: jwtClient,
};
const response = await write(request);
return response;
},
async clearSheet(jwtClient, spreadsheet_id, sheet_name) {
const clear = util.promisify(sheets.spreadsheets.values.clear);
const request = {
spreadsheetId: spreadsheet_id,
range: sheet_name
};
const response = await clear(request);
return response;
}
使用相同的jwt客户端,spreadsheetid,sheetname,第一次调用成功,第二次调用失败
await google.updateSheet(google_jwtClient, spreadsheet_id, sheet_name, data);
await google.clearSheet(google_jwtClient, spreadsheet_id, sheet_name);
{ 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.', domain: 'global', reason: 'unauthorized' }
'Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential.
表示您没有随请求发送凭据。
您没有将 jwtClient 应用于请求。
async clearSheet(jwtClient, spreadsheet_id, sheet_name) {
const clear = util.promisify(sheets.spreadsheets.values.clear);
const request = {
spreadsheetId: spreadsheet_id,
auth: jwtClient,
range: sheet_name
};
const response = await clear(request);
return response;
}