Closed
Description
I'm pretty sure the problem is entirely on my end. But I can't quite figure out what I'm doing wrong. In short, I'm unable to successfully use the Google Translate API. Despite constructing what appears to be a valid request message, I keep getting the same error, "Missing required field target":
Unhandled Exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Missing required field target [400]
Errors [
Message[Missing required field target] Location[ - ] Reason[badRequest] Domain[global]
]
Here's my F# code. I'm using Application Default Credentials from gcloud
, and calling the Google Speech API works. (Although that is using gRPC/veneer, so it's a very different codepath.)
// Google Translate .NET Client Library bug repro.
//
// NuGet Dependencies:
// Google.Apis.Translate.v2 (1.29.1.875)
// Newtonsoft.Json (10.0.3) -- Just for printing JSON objects.
open System.Collections.Generic
open Google.Apis.Auth.OAuth2
open Google.Apis.Services
open Google.Apis.Translate.v2
// Just for showing the serialized form of the API request body.
open Newtonsoft.Json
[<EntryPoint>]
let main argv =
printfn "Use Google Translate API"
let googleCreds = GoogleCredential.GetApplicationDefault()
let baseClient = new BaseClientService.Initializer(HttpClientInitializer = googleCreds)
let translateSvc = new TranslateService(baseClient)
let text = new List<string>()
text.Add("Help! I do not know Spanish and am lost in Mexico.")
let translateReq = new Data.TranslateTextRequest()
translateReq.Format <- "text"
translateReq.Q <- text
translateReq.Source <- "en"
translateReq.Target <- "es"
// Confirm the request object is constructed correctly.
printfn "%A: %A [%s->%s]" translateReq translateReq.Q translateReq.Source translateReq.Target
printfn "Request in JSON:\n%s" <| JsonConvert.SerializeObject(translateReq)
let reqObj = translateSvc.Translations.Translate(translateReq)
let respObj = reqObj.Execute()
printfn "Success! Response:\n%A" respObj
0
And typical output. Notice how the "target" field is found in the JSON serialization of the object.
$ dotnet run
Use Google Translate API
Google.Apis.Translate.v2.Data.TranslateTextRequest: seq ["Help! I do not know Spanish and am lost in Mexico."] [en->es]
Request in JSON:
{"format":"text","model":null,"q":["Help! I do not know Spanish and am lost in Mexico."],"source":"en","target":"es","ETag":null}
Unhandled Exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Missing required field target [400]
Errors [
Message[Missing required field target] Location[ - ] Reason[badRequest] Domain[global]
]
at Google.Apis.Requests.ClientServiceRequest`1.Execute()
Any ideas on what the issue could be?