2016-03-16 207 views
0

我使用創建憑據得到了一個新的API密鑰。但我沒有輸入任何計費details.Then我用下面的代碼來訪問翻譯API以文本使用Java中的Google Translate V2 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) 

在我獲得密鑰的同一天。我找不到原因。

回答

1

輸入您的結算明細後再試一次。您需要在API接受您的API密鑰之前這樣做。

相關問題