我在谷歌单子上有一些数据。我正在使用用于Node.js的Sheets API访问这些数据。对于身份验证,我使用Service来生成一个JWTClient。
我可以从sheets中访问数据,一切都很好。代码托管在Google灵活环境上。它对本地主机和GAE都很好。
唯一的问题是我需要提供用于身份验证的服务帐户密钥json。这在本地环境中很好,但在App上,它为服务帐户提供了一个违约凭证 (我使用的是相同的服务帐户)。因此,我认为我不应该手动提供凭据,让GAE负责服务器上的凭据。
但它不是自动身份验证,仍然是一个auth参数。
下面是我当前实现的sheets
const { promisify } = require('util');
const google = require('googleapis');
const auth = require('./auth');
const credentials = require('../configs/credentials');
const sheets = google.sheets('v4');
const getSheetValuesAsync = promisify(sheets.spreadsheets.values.get);
//region private
function readSheet(params) {
return getSheetValuesAsync(params);
}
//endregion private
//region public
async function get(sheetConfigName) {
const sheetConfig = credentials.sheets[sheetConfigName];
//This is theJWTClient which authenticates the api request currently.
//I want to remove this
const authClient = await auth.authorizeWithServiceAccount('serviceAccountName', [
'https://www.googleapis.com/auth/spreadsheets.readonly'
]);
console.log('Client received');
let params = {
auth: authClient, //I tried removing this parameter, but it didn't work
spreadsheetId: sheetConfig.id,
range: 'A:M'
};
let data = await readSheet(params);
return data;
}
//endregion public
module.exports = {
get: get
};
我所犯的错误如下:
{
code:403,
message: 'The request is missing a valid API key.
}
我已经为本地环境设置了GOOGLE_APPLICATION_CREDENTIALS
环境变量,它适用于google,但不适用于工作表。
那么,Application Default Credentials
只是用于google,还是可以与Sheets或任何其他驱动API一起使用?如果它也可以与驱动器API一起工作,那么如何才能做到呢?
发布于 2018-04-26 04:40:00
在谷歌的Node.js客户端包(28.x)的最新更新中,他们有了一种无需第二个包就能获得authClient的新方法。这是怎么做的-
const { google } = require('googleapis');
const authClient = await google.auth.getClient({
credentials: credentials,
scopes: scopes
});
credentials
只是服务帐户JSON对象。最好的部分是,如果不提供该属性,它将检查GOOGLE_APPLICATION_CREDENTIALS
环境变量,该变量将具有服务帐户键JSON的绝对路径,并自动将其视为身份验证。
您可以使用生成的authClient来设置google的auth参数,方法如下-
首先,当您想为不同的API调用提供不同的auth时。
const { google } = require('googleapis');
const sheets = google.sheets('v4');
sheets.spreadsheets.values.getAsync = promisify(sheets.spreadsheets.values.get);
sheets.spreadsheets.values.getAsync({
auth: authClient,
spreadsheetId: spreadsheetId,
range: range
});
或者您可以直接向sheets对象提供auth。对该对象的所有API调用都将使用相同的authClient,并且不需要auth参数。
const sheets = google.sheets({version:'v4',auth:authClient);
最好的方法是为所有Google调用设置auth默认值,然后您就不必将authClient传递给任何sheets、对象或API调用。
google.options({
auth: authClient
});
您仍然可以在工作表(或驱动器或任何其他对象) API对象或任何API调用中重写auth参数,以防您计划对不同的API使用不同的服务帐户。
为了在本地进行测试,您可以始终设置环境路径,这样就不必实际将服务帐户传递给google.auth
对象。我就是这么做的
let resolve = require('path').resolve;
if (process.env.NODE_ENV === 'production') {
//Set config based on production environment. It won't be set in local environment, unless you specially set it.
} else if (!process.env.GOOGLE_APPLICATION_CREDENTIALS) {
process.env.GOOGLE_APPLICATION_CREDENTIALS = resolve(
'./relative/path/to/the/json/key.json'
); //resolve will just convert relative path to the absolute path.
}
发布于 2018-03-30 18:12:00
如果您想使用Google,您需要提供一个OAuth服务帐户,如文档中所示。默认服务帐户主要用于从GCP (Google平台)中提取数据时使用。因此,您的计算引擎可以访问您的云数据存储,或者如果您的应用程序引擎可以访问您的云存储。
默认的服务帐户没有一个客户端机密JSON,它是Sheets API所需要的。
或者,如果要使用Google Sheets REST API,则可以为公共文档使用API密钥。
发布于 2018-03-31 15:25:46
Google服务帐户是用来访问Cloud的,不能为Sheets授予适当的范围。
由于App灵活的应用程序在Compute Engine VM上运行,所以您可以通过查询元数据服务器获得默认服务帐户的令牌,如下所示:
> curl "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token" -H "Metadata-Flavor: Google"
但是,当调用提供该令牌的Sheets时,它会产生以下错误:
{
"error": {
"code": 403,
"message": "Request had insufficient authentication scopes.",
"status": "PERMISSION_DENIED"
}
}
我相信您需要在用例中使用API密钥。
https://stackoverflow.com/questions/49563586
复制相似问题