我使用创建凭据获得了一个新的API密钥。但我没有输入任何结算明细。然后我使用以下代码访问翻译API以翻译文本
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());
}
}
}
作为输出我得到以下
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)
我在获得密钥的同一天使用了这个应用程序。我无法找到原因。
答案 0 :(得分:1)
输入结算明细后再试一次。您需要在API接受API密钥之前执行此操作。