我需要编写一个函数来使用包含凭证详细信息(从秘密管理器获得)的 json 文件将消息发布到 google pubsub。
**公共异步任务 PublisMessage(ApiRequestHeaders MetaData,PubSubRequest pubSubModel){字符串 jsonPath = Environment.GetEnvironmentVariable(“GOOGLE_APPLICATION_CREDENTIALS”);
GoogleCredential googleCredential = null;
using (var jsonStream = new FileStream(jsonPath, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
googleCredential = GoogleCredential.FromStream(jsonStream);
}
var createSettings = new PublisherClient.ClientCreationSettings(credentials: googleCredential.ToChannelCredentials());
TopicName topicName = new TopicName(pubSubModel.ProjectId, pubSubModel.TopicName);
try
{
PublisherClient _publisher = await PublisherClient.CreateAsync(topicName,
clientCreationSettings: createSettings);
var _message = await GetPublisMessage(pubSubModel.DocumentId, MetaData);
var acknowledgmentNumber = await _publisher.PublishAsync(_message); //Permission denied error
return true;
}
catch (Exception ex)
{
_logger.Error("PushMessageToPubsub Error : " + ex);
return false;
}
**