为什么我使用google
我用Google Analytics Reporting API调用google-api-nodejs-client来显示博客中的访问次数。
此博客托管在Google App Engine标准环境中。
在开发中,我正在使用Application Default Credentials验证我的API调用。我使用我专门为分析目的创建的帐户服务的凭据下载了JSON文件,将文件设置为Google_Application_Credentials环境变量,一切正常。我可以从Google Analytics获取数据并将其显示在网站中。
但这不适用于生产。我想getClient()
它没有获得该环境中的凭据。
注意事项:1)我没有使用服务帐户的凭据上传下载的JSON文件(我认为这样做是违反直觉和不安全的,而且根据我在文档中的理解,GCP能够自动处理使用API身份验证);
const {google} = require("googleapis");
async function main () {
// This method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
// environment variables.
const auth = await google.auth.getClient({
// Scope of the analytics reporting,
// with only reading access.
scopes: '.readonly',
});
// Create the analytics reporting object
const analyticsreporting = await google.analyticsreporting({
version: 'v4',
auth: auth,
});
// Fetch the analytics reporting
const res = await analyticsreporting.reports.batchGet({...});
return res.data;
}
我已经用完了选项。有人可以帮我弄这个吗?
回答如下:这是默认范围和应用程序默认凭据的问题。默认情况下,如果您不创建新的服务帐户,您将从GCE元数据服务获得“应用程序默认凭据”:https://cloud.google/docs/authentication/production#auth-cloud-implicit-nodejs
这些凭证通常只有cloud-platform
范围,并且范围集不能更改(截至今天)。为了使这项工作,你有几个选择。
- 您可以创建新的服务帐户,下载服务帐户密钥,并使用
keyFile
方法选项中的getClient
属性来引用密钥。如果你这样做,你传递给getClient
的范围将得到尊重。 - 您可以使用运行GAE应用程序的服务帐户可用的范围。我没有亲自尝试过,但理论上应该是可能的。
祝你好运!
为什么我使用google
我用Google Analytics Reporting API调用google-api-nodejs-client来显示博客中的访问次数。
此博客托管在Google App Engine标准环境中。
在开发中,我正在使用Application Default Credentials验证我的API调用。我使用我专门为分析目的创建的帐户服务的凭据下载了JSON文件,将文件设置为Google_Application_Credentials环境变量,一切正常。我可以从Google Analytics获取数据并将其显示在网站中。
但这不适用于生产。我想getClient()
它没有获得该环境中的凭据。
注意事项:1)我没有使用服务帐户的凭据上传下载的JSON文件(我认为这样做是违反直觉和不安全的,而且根据我在文档中的理解,GCP能够自动处理使用API身份验证);
const {google} = require("googleapis");
async function main () {
// This method looks for the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
// environment variables.
const auth = await google.auth.getClient({
// Scope of the analytics reporting,
// with only reading access.
scopes: '.readonly',
});
// Create the analytics reporting object
const analyticsreporting = await google.analyticsreporting({
version: 'v4',
auth: auth,
});
// Fetch the analytics reporting
const res = await analyticsreporting.reports.batchGet({...});
return res.data;
}
我已经用完了选项。有人可以帮我弄这个吗?
回答如下:这是默认范围和应用程序默认凭据的问题。默认情况下,如果您不创建新的服务帐户,您将从GCE元数据服务获得“应用程序默认凭据”:https://cloud.google/docs/authentication/production#auth-cloud-implicit-nodejs
这些凭证通常只有cloud-platform
范围,并且范围集不能更改(截至今天)。为了使这项工作,你有几个选择。
- 您可以创建新的服务帐户,下载服务帐户密钥,并使用
keyFile
方法选项中的getClient
属性来引用密钥。如果你这样做,你传递给getClient
的范围将得到尊重。 - 您可以使用运行GAE应用程序的服务帐户可用的范围。我没有亲自尝试过,但理论上应该是可能的。
祝你好运!