2016-05-14 175 views
1

我使用谷歌翻译的API,以便使用文本到语音,但是当我运行这个代码,并把我的关键API在键: 私有静态最终字符串BASE_URL =“https:// www.googleapis.com/language/translate/v2?key=[key] & q = hello%20world & source = en & target = de“;谷歌翻译api 403禁止在java

public static void main(String[] args) { 
    say("Hallo Welt"); 

} 
public static void say(String text) { 
    try { 
     File f = new File("translate_tts.mp3"); 
     String sentence = URLEncoder.encode(text,"UTF-8"); 
     String urlString = MessageFormat.format(BASE_URL, sentence,"en"); 
     BinaryResource res = new Resty().bytes(new URI(urlString)); 
     res.save(f); 

     FileInputStream in = new FileInputStream(f); 
     Audio audio = Audio.getInstance(); 
     audio.play(in); 

     Player p = new Player(in); 

     p.play(); 

     p.close(); 
     f.delete(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (URISyntaxException e) { 
     e.printStackTrace(); 
    } catch (JavaLayerException e) { 
     e.printStackTrace(); 
    } 

} 

我有错误403和服务器上的错误返回HTTP响应代码。你能帮我修复它吗?

回答

0

Google多年前关闭了它的翻译webservice。目前只能通过网页访问。

+0

从api可以使用文本到语音的api名称是什么?我已经知道谷歌翻译API包括文本到语音(详细信息,我使用这个API来检测和讲越南文。 –