Ho cercato ovunque in Internet per l'utilizzo dell'API di Google Translate ma non sono riuscito a trovare tutorial o spiegazioni sulla discesa. Quindi, ecco cosa ho fatto:
Nella mia console API di Google ho generato una chiave sotto accesso API pubblica con il mio impronta digitale SHA1 utilizzando la risposta this. Ecco come la mia console API assomiglia: Utilizzo dell'API di Google Traduttore in Android
In studio Android ho costruire e inviare la mia richiesta utilizzando biblioteca OkHttp con questo codice:
OkHttpClient client = new OkHttpClient();
String apiKey = "My API key";
String apiLangSource = "en";
String apiLangTarget = "de";
String apiWord = "Hello";
String googleApiUrl = "https://www.googleapis.com/language/translate/v2?key=" + apiKey + "&source=" + apiLangSource + "&target=" + apiLangTarget + "&q=" + apiWord;
Request request = new Request.Builder().url(googleApiUrl).build();
Log.d(TAG, "API STRING" + googleApiUrl);
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.d(TAG , "HTTP CALL FAIL");
}
@Override
public void onResponse(Response response) throws IOException {
Log.d(TAG , response.body().string());
}
});
Funziona benissimo, ma sulla risposta ottengo:
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "ipRefererBlocked",
"message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.",
"extendedHelp": "https://console.developers.google.com"
}
],
"code": 403,
"message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
}
}
Qual è il problema qui? La mia API è impostata correttamente? Sto facendo la chiamata correttamente (ho visto alcune librerie ma con la guida)? Questo modo ragionevole di usare questa libreria? Che cosa significa questo?
"There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
Penso che ci siano alcune chiamate demo disponibili gratuitamente e questo non è il problema qui.
Immagino che la tua configurazione dell'API non sia ben rifinita.Posso vedere risposta 'restrizione configurata sulla tua chiave API e la richiesta non corrisponde' –
Quando rimuovo l'impronta digitale e il pacchetto funziona. Quindi è davvero un errore di autenticazione. In che modo Google verifica la mia impronta digitale e il pacchetto? – BabbevDan
Sto incontrando lo stesso problema. @DannyBabbev, hai trovato una soluzione per questo? – Flo