简体   繁体   中英

Google translate api 403 forbidden in java

I use API from Google Translate in order to use text to speech, but when i running this code and put my key api in key: private static final String BASE_URL="https://www.googleapis.com/language/translate/v2?key=[key]&q=hello%20world&source=en&target=de";

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();
    }

}

I have error 403 and wrong on server returned HTTP response code. Can you help me to fix it

Google shut down its translate webservice years ago. Currently it is only available via the webpage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM