I got a new API key using create credentials. But I didnt enter any billing details.Then I used the following code to access the translator API in order to translate a text
package traanslatorapi;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.model.TranslationsListResponse;
import com.google.api.services.translate.model.TranslationsResource;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author User
*/
public class TraanslatorApi {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
Translate t = null;
try {
t = new Translate.Builder(
com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(), com.google.api.client.json.gson.GsonFactory.getDefaultInstance(), null)
//Need to update this to your App-Name
.setApplicationName("OCRProject")
.build();
} catch (GeneralSecurityException ex) {
Logger.getLogger(TraanslatorApi.class.getName()).log(Level.SEVERE, null, ex);
}
Translate.Translations.List list = t.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");
//Set your API-Key from https://console.developers.google.com/
list.setKey("AIzaSyCX2O-pteDLJLeMivT47kD9pucEv67QECQ");
TranslationsListResponse response = list.execute();
for (TranslationsResource tr : response.getTranslations()) {
System.out.println(tr.getTranslatedText());
}
}
}
As the output I got the following
run:
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code": 403,
"errors": [
{
"domain": "usageLimits",
"message": "Daily Limit Exceeded",
"reason": "dailyLimitExceeded"
}
],
"message": "Daily Limit Exceeded"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at traanslatorapi.TraanslatorApi.main(TraanslatorApi.java:47)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
I used this application on the same day that I obtained the key. I can't find the reason for this.
Give it another try after entering your billing details. You need to do that before the API will accept your API key.