Problem with authentication on Android

1,151 views
Skip to first unread message

Web Map

unread,
Jul 23, 2015, 4:24:24 PM7/23/15
to Google Translate API Developer Forum
I use Google Translate API v2 on Android. When I generate Public API access with no certificate fingerprint and package name everything runs fine. The problem appears when I use my SHA1 fingerprint of the both release and debug certificate. I am using the correct package name  and fingerprint but I get this error returned by the server:
{
"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."
}
}

So, there is some sort of authentication problem but I am sure that I am using correct information for the fingerprint and package. Here is how I build my requests, using the OkHttp library  :
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());

       
}
   
});

Am I missing something with the authentication, fingerprints etc. ? 

Patrice (Google Cloud Support)

unread,
Jul 23, 2015, 7:04:34 PM7/23/15
to Google Translate API Developer Forum
Hi!

Normally, Google Groups are less suited for 1 on 1 type support (questions like yours, basically) and more suited towards discussions on workarounds, or discussions about good usage of the system and/or platform. 

I would suggest posting this on Stack Overflow, where we do monitor questions and answer what we can.

In the meantime, if you look at your error message, it says to "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.", so I would suggest going into your API key configurations in the dev console and making sure if this IP or referer is properly allowed. 

Are you sure you follow the instructions here to the letter?

Cheers!

Web Map

unread,
Jul 24, 2015, 2:48:32 AM7/24/15
to Google Translate API Developer Forum
Hi Patrice
I have posted 2 questions on stackoverflow and they both have been unanswered. Here are the linkshttp://stackoverflow.com/questions/31586345/usage-of-google-translate-api-in-androidhttp://stackoverflow.com/questions/31589308/how-does-google-verify-android-sha1-fingerprints-and-packages . I have carefully followed the steps provided here and it is still not working. That's why I posted my thread here, neither the community knows nor the instructions provided by Google work!

Patrice (Google Cloud Support)

unread,
Jul 24, 2015, 6:30:25 PM7/24/15
to Google Translate API Developer Forum
Hi again,

I'd just like to point out your questions were asked yesterday. While we do monitor and answer on stack, there is no promise of resolution. We do our best, and it's usually tied to the reception the question gets on stack.

In general though, when you create such keys, you can always set your referrer or IP. Making sure this referrer is your domain name (or your application url) should fix this. Also making sure the API is enabled on the project (by going to "API & auth" from console.developers.com, then going to find the translate API and enabling it if it isn't already) would be necessary.

Let me know if these fix your issues.

Cheers!
Message has been deleted

Web Map

unread,
Jul 25, 2015, 4:23:32 PM7/25/15
to Google Translate API Developer Forum
Hi Patrice,
This referrer IP or URL is used in Browser keys, to elaborate "API requests are sent directly to Google from your clients' browsers". I am making an Android application here! Problem is, this is still not working...

Patrice (Google Cloud Support)

unread,
Jul 27, 2015, 11:31:45 AM7/27/15
to Google Translate API Developer Forum
Hi,

Indeed, this is for browser keys and not a Android app key. I've never done android dev myself (more of a backend dev), so when I saw your issue I researched how it could be circumvented, and I found that actually some people used exactly that when getting a referrer error on android.

I've never tried it myself, but having seen it used by other android developers I figured it could be useful here. I guess that your last message means you actually tried it but it still came up short?


Ben

unread,
Nov 4, 2015, 7:37:04 PM11/4/15
to Google Translate API Developer Forum
Hi,
I'm facing the same problem I guess. Did you manage to fix it?

Monica Tudora

unread,
Mar 30, 2016, 12:04:26 PM3/30/16
to Google Translate API Developer Forum
Hello,

Same problem here, i managed to make it work using a REST call and a browser key but i want to use the android translate api. It is not very well documented so i tried to do my own implementation but i keep on receiving the 403 error. Any suggestions or i should remain to the Rest call. Thank you.

GoogleClientRequestInitializer googleClientRequestInitializer = new TranslateRequestInitializer(mApiKey);
translateBuilder = new Translate.Builder(
new com.google.api.client.http.javanet.NetHttpTransport()
, JacksonFactory.getDefaultInstance(), null)
.setApplicationName(getResources().getString(R.string.app_name))
.setGoogleClientRequestInitializer(googleClientRequestInitializer)
.build();
Translate.Translations.List list = translateBuilder.new Translations().list(
Arrays.asList(
//Pass in list of strings to be translated
"Hello World",
"How to use Google Translate from Java"),
//Target language
"ES");
list.setKey(mApiKey);
TranslationsListResponse response = list.execute();


Nicolas Perez

unread,
May 19, 2016, 10:28:55 AM5/19/16
to Google Translate API Developer Forum
the way I solved it was setting in the property "referer" of the http-header the name of my website.

like this:

list.getRequestHeaders().set("referer", "www.misite.com");
Reply all
Reply to author
Forward
0 new messages