We are developing TTS proxy service where customer can come and configure their Google Cloud TTS credentials. And then they can use are endpoint to generate the text to speech.
For building the TextToSpeechClient client, we need to first initialize GoogleCredential object and while initializing the GoogleCredential object I have seen some example that uses CreateScoped(“https://www.googleapis.com/auth/cloud-platform”) and some don’t using it.
Is there any specific reason for using CreateScoped(“https://www.googleapis.com/auth/cloud-platform”) while initializing GoogleCredential? What if we don’t use CreateScoped.
Is there any performance impact CreateScoped?
Sample code of initializing the google client.
private async Task InitClient(int busNo, TextToSpeechConfig textToSpeechConfig)
{
TextToSpeechClient client = null;
string googleConfig = JsonConvert.SerializeObject(textToSpeechConfig.Parameters);
GoogleTTSConfig googleTTSConfig = JsonConvert.DeserializeObject(googleConfig);
string providercacheKey = CacheUtils.GetCacheKeyForProviderClient(busNo, textToSpeechConfig);
_cacheProvider.GetCacheValue(providercacheKey,out client);
if (client == null)
{
if (validateConfig(googleTTSConfig))
{
string base64ServiceAccount = googleTTSConfig.Base64ServiceAccount;
byte[] byteArray = Convert.FromBase64String(base64ServiceAccount);
var serviceAccount = Encoding.UTF8.GetString(byteArray);
serviceAccount = serviceAccount.Replace(@"\", @"");
GoogleCredential googleCred = GoogleCredential.FromJson(serviceAccount.ToString()).CreateScoped("https://www.googleapis.com/auth/cloud-platform");
this.logClientDetails(serviceAccount);
ChannelCredentials channelCreds = googleCred.ToChannelCredentials();
TextToSpeechClientBuilder clientBuilder = new TextToSpeechClientBuilder()
{
ChannelCredentials = channelCreds,
Endpoint = "texttospeech.googleapis.com"
};
client = await clientBuilder.BuildAsync();
_cacheProvider.SetCacheValue(providercacheKey, client, TimeSpan.FromMinutes(5));
}
else
{
_logger.LogError($"Invalid GoogleTTSConfig");
throw new Exception("Invalid GoogleTTSConfig");
}
}
else {
_logger.LogInformation($"Client found in cache");
}
return client;
}
I tried with both CreateScoped and with using CreateScoped I did not found any difference.