将自定义凭据与Stackdriver Logging API C#客户端库一起使用

时间:2018-11-06 15:52:02

标签: c# .net logging stackdriver google-cloud-stackdriver

我想使用GoogleCredential对象(或类似对象)以便使用一些自定义凭据而不是默认应用程序凭据来创建Stackdriver日志记录客户端对象(LoggingServiceV2Client类的实例)

我看不到LoggingServiceV2Client.Create方法的适当重载,但是该方法的文档字符串指出:

  

同步创建一个   Google.Cloud.Logging.V2.LoggingServiceV2Client,为   所有未指定的设置,并创建一个连接到   在必要时给定端点提供应用程序默认凭据。   请参阅示例以了解如何使用自定义凭据

这暗示有可能吗?

我在任何地方的文档中都找不到自定义凭据示例。我看到的唯一示例(例如this)仅从GOOGLE_APPLICATION_CREDENTIALS环境变量中读取 default 应用程序凭据,我希望避免使用该

2 个答案:

答案 0 :(得分:2)

可能,但远非显而易见。

将这两个using语句添加到.cs顶部:

using Google.Apis.Auth.OAuth2;
using Grpc.Auth;

然后像这样实例化客户端:

var credential = GoogleCredential.FromFile(jsonPath)
    .CreateScoped(LoggingServiceV2Client.DefaultScopes);
var channel = new Grpc.Core.Channel(
    LoggingServiceV2Client.DefaultEndpoint.ToString(),
    credential.ToChannelCredentials());
var client = LoggingServiceV2Client.Create(channel);

答案 1 :(得分:0)

我已经很喜欢@Jeffrey Rennie。就我而言,我使用的是 Cloud Text-to-Speech ,我必须使用以下代码:

用途:

using Google.Apis.Auth.OAuth2;
using Google.Cloud.TextToSpeech.V1;
using Grpc.Auth;

代码:

// Setting up credentials
string jsonPath = @"D:\my-test-project-0078ca7c0f8c.json";
var credential = GoogleCredential.FromFile(jsonPath).CreateScoped(TextToSpeechClient.DefaultScopes);
var channel = new Grpc.Core.Channel(TextToSpeechClient.DefaultEndpoint.ToString(), credential.ToChannelCredentials());

// Instantiate a client
TextToSpeechClient client = TextToSpeechClient.Create(channel);

// Perform the Text-to-Speech request, passing the text input with the selected voice parameters and audio file type 
var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
{
    Input = new SynthesisInput() { Text = "My test sentence" },
    Voice = new VoiceSelectionParams() { LanguageCode = "en-US", SsmlGender = SsmlVoiceGender.Male },
    AudioConfig = new AudioConfig { AudioEncoding = AudioEncoding.Mp3 };
});

已安装NuGet软件包
Google.Cloud.TextToSpeech.V1 -Pre
Google.Apis.Auth