我在测试期间多次调用API
就好了但是今天当我让很多客户使用它时,它开始返回以下错误:
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"
}
我的代码如下:
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
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.util.Collections;
import java.util.List;
public class GoogleTranslateAPI
{
private static final String APPLICATION_NAME = "MY-APPLICATION-NAME";
private static final String API_KEY = "MY-API-KEY";
private static final String DEFAULT_SOURCE_LANGUAGE = "en";
public static String translate(String input, String targetLanguage) throws Exception
{
return translate(input, null, targetLanguage);
}
@SuppressWarnings("WeakerAccess")
public static String translate(String input, String sourceLanguage, String targetLanguage) throws Exception
{
// Watermark the Google translations
if (!targetLanguage.equals(DEFAULT_SOURCE_LANGUAGE))
{
input += "<br>Translated with Google Translate. Please read the English version for most accuracy.";
}
Translate.Translations.List translationsList = buildTranslationsList(input, targetLanguage, sourceLanguage);
TranslationsListResponse response = translationsList.execute();
List<TranslationsResource> translations = response.getTranslations();
int translationsCount = translations.size();
if (translationsCount != 1)
{
throw new IllegalStateException("Unexpected translations count: " + translationsCount);
}
TranslationsResource translationsResource = translations.get(0);
return translationsResource.getTranslatedText();
}
private static Translate.Translations.List buildTranslationsList(String input, String targetLanguage, String sourceLanguage) throws Exception
{
Translate translate = buildTranslate();
Translate.Translations.List translationsList = translate.new Translations()
.list(Collections.singletonList(input), targetLanguage)
.setSource(sourceLanguage);
translationsList.setKey(API_KEY);
return translationsList;
}
private static Translate buildTranslate() throws Exception
{
return new Translate.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
GsonFactory.getDefaultInstance(), null)
.setApplicationName(APPLICATION_NAME)
.build();
}
}
我检查了我的Google Console
并说:
There is no traffic for this time period.
我更改了时间段并明确使用了API
,但没有数据。此外,我在开始使用API
之前启用了结算并输入了有效的结算信息。翻译API
表示已启用。当然,我为项目生成了API key
,并在源代码中替换了占位符(API key
和project name
)。
我似乎正在做正确的事情,但Google
不会让我继续使用它,即使他们可能会向我收取使用费用,因此它不会返回错误至少如果我的配额用完了。由于我的帐户中没有数据显示,我甚至无法看到我使用或已经离开了多少配额。
如何解决此问题?