如何为 Dialogflow 集成验证 Google 凭据

How to authenticate Google Credentials for Dialogflow Integration

我想将我的 C# 系统与 Google Dialogflow 集成。 所以我正在尝试使用 Jon 先生给我看的代码:

但我遇到了这个问题:

The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

我已经下载了我的 Dialogflow 项目服务帐户密钥 JSON 文件。 我正在尝试使用此代码进行身份验证:

// Some APIs, like Storage, accept a credential in their Create() method.
// Explicitly use service account credentials by specifying the private key file.
GoogleCredential credential = GoogleCredential.FromFile(theServiceAccountJSONFilePath);
StorageClient storage = StorageClient.Create(credential);
// Make an authenticated API request.
PagedEnumerable<Buckets, Bucket> buckets = storage.ListBuckets(theProjectID);
foreach (Bucket bucket in buckets)
{
    Console.WriteLine(bucket.Name);
}
return null;

我从这个 link 得到了代码:Setting Up Authentication for Server to Server Production Applications

问题是代码对我来说遇到了这个问题:

dialogflow-ixksso@maintest-vskxxy.iam.gserviceaccount.com does not have storage.buckets.list access to project 160007643358.

我在 'Google Cloud Platform' 上使用免费选项。也许免费选项不允许以这种方式进行身份验证。

我没有太多这方面的经验,所以任何建议将不胜感激。

如评论中所述,大多数情况下最简单的方法是指定 GOOGLE_APPLICATION_CREDENTIALS 环境变量以引用 JSON 文件。

如果您需要以其他方式加载凭据,您可以使用客户端生成器非常灵活地指定凭据:

来自 ICredential

ICredential credential = LoadCredentialFromSomewhere();
var client = new AgentsClientBuilder
{
    TokenAccessMethod = credential.GetAccessTokenForRequestAsync 
}.Build();

从路径到 JSON 文件

var client = new AgentsClientBuilder
{
    CredentialsPath = "/path/to/serviceaccount.json"
}.Build();

来自 JSON 你已经有一个字符串

string json = LoadJsonFromSomewhere();
var client = new AgentsClientBuilder
{
    JsonCredentials = json
}.Build();