Ich muss Text von einer Sprache in eine andere übersetzen. Ich verwende Google-Übersetzung API. Allerdings kann ich keine Authentifizierung zum Arbeiten bekommen. Ich möchte Authentifizierungsinformationen über Code bereitstellen, ich möchte mich nicht automatisch mit globalen Variablen authentifizieren (ich frage mich immer noch, wie das eine gute Idee ist).Nicht authentifizierbar: Kein JSON-Eingang gefunden
Obwohl es eine einfache Methode zum Festlegen des API-Schlüssels gibt, gibt es keine Methode, die Anmeldeinformationen auf einfache Weise anzugeben. Also entschied ich mich für die Authentifizierung mit JSON, die ich beim Erstellen eines Dienstschlüssels erhalten habe.
{
"type": "service_account",
"project_id": "toto",
"private_key_id": "tata",
"private_key": "titi",
"client_email": "tete",
"client_id": "tutu",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/toto"
}
Dann im scala Code:
val cred = GoogleCredentials.fromStream(JSON_AUTHENTICATION_INPUT_STREAM)
TranslateOptions.newBuilder().setCredentials(cred)
Und natürlich, bekomme ich folgende Fehlermeldung:
java.lang.IllegalArgumentException: no JSON input found
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:125)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:49)
at com.google.api.client.json.JsonParser.startParsing(JsonParser.java:223)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:380)
at com.google.api.client.json.JsonParser.parse(JsonParser.java:355)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:87)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.fromStream(GoogleCredential.java:250)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.fromStream(GoogleCredential.java:226)
at services.TranslatorBaseImpl.<init>(Translator.scala:58)
So what? JSON-Authentifizierung funktioniert nicht? Wie kann ich über Code authentifizieren?
Ich suchte nach Stunden im offiziellen Dokument, aber es ist nutzlos für solch einen ... fortgeschrittenen ... Fall.
Vielen Dank für Ihre Antworten, Kuss