Using Ore API to check for updates

As the title said I want to use the Ore API to implement an UpdateChecker inside my plugin, so when a new version is released users will get notified. However I am getting a 403 error when using the API, but in the documentation there’s nothing about authentication. I’m trying to call this URL
https://ore.spongepowered.org/api/v1/projects/universeguard/versions?limit=1&offset=0
which gives me a result when navigating to it via browser. However it gives me a 403 error when the plugin tries to poll that URL. This is the code I use to poll that URL

try {
            StringBuilder result = new StringBuilder();
            URL url = new URL(PluginSettings.UPDATE_URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            BufferedReader reader = new BufferedReader((new InputStreamReader(connection.getInputStream())));
            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            reader.close();
            JsonArray versions = JsonUtils.parse(result.toString(), JsonArray.class);
            float latestVersion = versions.get(0).getAsJsonObject().get("name").getAsFloat();
            if (latestVersion > Settings.REGION_VERSION) {
                EventUtils.fire(new UpdateAvailableEvent(latestVersion));
            } else {
                LogUtils.log(Level.WARN, "YOU HAVE THE LATEST VERSION!");
            }
        } catch (IOException ex) {
            LogUtils.exception(ex);
        }

where PluginSettings.UPDATE_URL is the API URL I’m trying to poll and Settings.REGION_VERSION is the running plugin version. Is anything wrong with it?

You can look at my implementation GitHub - DosMike/SpongePluginVersionChecker: Minimalistinc include for fully automatic version checking agains the sponge ore repository using api v2 (or even just shade it into your plugin if you want to)

From what i can see you: 1) Don’t send a User-Agent, 2) Don’t use a session (just seen you’re using api v1, i think that doesn’t require sessions)

Also you should be able to pass the intput stream into a JsonParser instead of collecting the json into a string first.

All prod connections to Ore require a valid format user agent.

Yep, there are probably better ways to parse the result. Also where can I find the v2 API reference? Since I can see only v1 in there

Yes, setting the Chrome user agent worked. However, is there a way to set a “custom” user agent or something valid without using one for a specific browser?

Yes. As long as it follows a valid User-Agent format it should work okay.

If a custom one doesn’t work let me know what you are using I may need to whitelist it or have a set one for Ore.

I ended up using this string

<ID>/<Version>(Sponge <API VERSION>)

And it worked :smile:

you can see the v2 endpoints if you go to ore, scroll down and click on API. ore-stagin is more up-to-date i think. but be aware, iirc the v2 API might still change

1 Like