Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Translation API tip 403 What parameters are missing #605

Open
tjuzdy opened this issue Apr 12, 2024 · 1 comment
Open

Use Translation API tip 403 What parameters are missing #605

tjuzdy opened this issue Apr 12, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@tjuzdy
Copy link

tjuzdy commented Apr 12, 2024

When I call the API in Java, it tip 403
java program as follows

public final class Translator {
private static String urlApi = "https://translate.fedilab.app/translate";

public static String translate(@NonNull String from, @NonNull String to, @NonNull String request) {
    if (from == null) {
        throw new NullPointerException("from is marked non-null but is null");
    } else if (to == null) {
        throw new NullPointerException("to is marked non-null but is null");
    } else if (request == null) {
        throw new NullPointerException("request is marked non-null but is null");
    } else {
        try {
            URL url = new URL(urlApi);
            HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
            httpConn.setRequestMethod("POST");
            httpConn.setRequestProperty("accept", "application/json");
            httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpConn.setDoOutput(true);
            OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
            writer.write("q=" + URLEncoder.encode(request, "UTF-8") + "&source=" + from + "&target=" + to + "&format=text");
            writer.flush();
            writer.close();
            httpConn.getOutputStream().close();
            if (httpConn.getResponseCode() / 100 != 2) {
                throw new BadTranslatorResponseException(httpConn.getResponseCode(), urlApi);
            } else {
                InputStream responseStream = httpConn.getInputStream();
                Scanner s = (new Scanner(responseStream)).useDelimiter("\\A");
                String response = s.hasNext() ? s.next() : "";
                return ((TranslateResponse)JsonUtil.from(response, TranslateResponse.class)).getTranslatedText();
            }
        } catch (Exception var9) {
            var9.printStackTrace();
            throw new RuntimeException(var9);
        }
    }
}

public static String translate(@NonNull Language from, @NonNull Language to, @NonNull String request) {
    if (from == null) {
        throw new NullPointerException("from is marked non-null but is null");
    } else if (to == null) {
        throw new NullPointerException("to is marked non-null but is null");
    } else if (request == null) {
        throw new NullPointerException("request is marked non-null but is null");
    } else {
        return to != Language.NONE && from != to ? translate(from.getCode(), to.getCode(), request) : request;
    }
}

public static String translate(@NonNull Language to, @NonNull String request) {
    if (to == null) {
        throw new NullPointerException("to is marked non-null but is null");
    } else if (request == null) {
        throw new NullPointerException("request is marked non-null but is null");
    } else {
        return to == Language.NONE ? request : translate("auto", to.getCode(), request);
    }
}

private Translator() {
    throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

public static void setUrlApi(String urlApi) {
    Translator.urlApi = urlApi;
}

}

@github-actions github-actions bot added the enhancement New feature or request label Apr 12, 2024
@pierotofy
Copy link
Member

Check https://github.com/dynomake/libretranslate-java for bindings to use LibreTranslate in Java?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants