I have some problems to integrate Google API Translate. Add to gradle configuration this dependecies:
compile 'com.google.apis:google-api-services-translate:v2-rev41-1.20.0'
compile ('com.google.api-client:google-api-client-android:1.20.0') {
exclude module: 'httpclient'
}
compile ('com.google.http-client:google-http-client-gson:1.20.0') {
exclude module: 'httpclient'
}
And use this sample to translate some text:
final Translate translate = new Translate.Builder(
AndroidHttp.newCompatibleTransport(), AndroidJsonFactory.getDefaultInstance(),
new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
Log.d(TAG, "Http requst: " + httpRequest);
}
})
.setTranslateRequestInitializer(new TranslateRequestInitializer("ANDROID_API_KEY"))
.build();
try {
String shot = PreferenceManager.getLanguage(App.getInst()).getLanguageShot();
Translate.Translations.List request = translate.translations().list(Arrays.asList(text), shot);
TranslationsListResponse tlr = request.execute();
List<TranslationsResource> list = tlr.getTranslations();
result = list.get(0).getTranslatedText();
} catch (IOException e) {
e.printStackTrace();
}
But I always receive IOException:
{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."}}
But, if I use Browser key, that work success. Why I can't use my android API key in this feature? Any idea?