Search code examples
vb.netgoogle-cloud-platformgoogle-translateapi-key

vb.net - google.cloud.translation.v2, how to specify API key?


I'm trying to use the google.cloud.translation.v2 API, (specifically V2 because I have read that V3 does not support API key but only service accounts) and I am not able to find how to specify my API key.

My code so far is this, and when I run it I get the error message by Google that it requires authentication..

Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click
    Dim client As TranslationClient = TranslationClient.Create()
    Dim result As TranslationResult = client.TranslateText(TextBox4.Text, LanguageCodes.English, LanguageCodes.German, TranslationModel.Base)
    TextBox5.Text = result.TranslatedText
End Sub

Ive been searching for hours but cannot find how to specify the API key.


Solution

  • Finally I found an answer to my problem:

        Dim service As New TranslateService(New BaseClientService.Initializer With {.ApiKey = "AIzaxxxxxxxxxxxxxxxxxxxxx-xxxxmI"})
        Dim client As New TranslationClientImpl(service)       
        Dim result As TranslationResult = client.TranslateText(TextBox4.Text, LanguageCodes.English)
        TextBox5.Text = result.TranslatedText