简体   繁体   English

如何验证用于Dialogflow集成的Google凭据

[英]How to authenticate Google Credentials for Dialogflow Integration

I want to integrate my C# system with Google Dialogflow. 我想将我的C#系统与Google Dialogflow集成。 So I'm trying to use the code Mr. Jon showed me over here: 因此,我正在尝试使用乔恩先生在此处向我展示的代码:

How to import a dialogflow zip using an API 如何使用API​​导入dialogflow zip

But I'm getting this problem: 但是我遇到了这个问题:

The Application Default Credentials are not available. 应用程序默认凭据不可用。 They are available if running in Google Compute Engine. 如果它们在Google Compute Engine中运行,则可用。 Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. 否则,必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS,指向指向定义凭据的文件。 See https://developers.google.com/accounts/docs/application-default-credentials for more information. 有关更多信息,请参见https://developers.google.com/accounts/docs/application-default-credentials

I've downloaded my Dialogflow Project Service Account Key JSON File. 我已经下载了Dialogflow项目服务帐户密钥JSON文件。 And I'm trying to use this code to Authenticate: 我正在尝试使用以下代码进行身份验证:

// 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;

I got that code from this link: Setting Up Authentication for Server to Server Production Applications 我从以下链接获得了该代码: 为服务器到服务器生产应用程序设置身份验证

Problem is that code runs into this problem for me: 问题是代码对我来说遇到了这个问题:

dialogflow-ixksso@maintest-vskxxy.iam.gserviceaccount.com does not have storage.buckets.list access to project 160007643358. dialogflow-ixksso@maintest-vskxxy.iam.gserviceaccount.com没有对项目160007643358的storage.buckets.list访问。

I am on the free option on my 'Google Cloud Platform'. 我可以在“ Google云平台”上使用免费选项。 Maybe the free option doesn't allow authenticating in this way. 也许free选项不允许以这种方式进行身份验证。

I don't have a lot of experience with this, so any advice would be appreciated. 我对此没有很多经验,所以任何建议都将不胜感激。

As noted in comments, the simplest way in most cases is to specify the GOOGLE_APPLICATION_CREDENTIALS environment variable to refer to the JSON file. 如评论中所述,在大多数情况下,最简单的方法是指定GOOGLE_APPLICATION_CREDENTIALS环境变量以引用JSON文件。

If you need to load the credential some other way, you can use the client builder to specify credentials quite flexibly: 如果您需要以其他方式加载凭据,则可以使用客户端构建器灵活地指定凭据:

From an ICredential 来自凭证

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

From a path to a JSON file 从JSON文件的路径

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

From JSON you already have as a string 通过JSON,您已经拥有一个字符串

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM