Question

So I am trying to get information of various images, for which I will use the Imgur API through Java.

I have found a library: https://github.com/fernandezpablo85/scribe-java,

but when trying the ImgUrTest.java @ https://github.com/fernandezpablo85/scribe-java/blob/master/src/test/java/org/scribe/examples/ImgUrExample.java, I get the following stacktrace:

Exception in thread "main" org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: 'OAuth Verification Failed: The consumer_key "<Client-ID>" token "" combination does not exist or is not enabled.'
    at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:41)
    at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:27)
    at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:64)
    at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:40)
    at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:45)
    at ImgUrExample.main(ImgUrExample.java:31)

where <Client-ID> is my client id, as found on ImgUr's page.

I have checked that my Client Id and Client Secret are correct, I have tried making multiple apps on the ImgUr site, none of which work.

Edit: This code works:

URL imgURL = new URL(YOUR_REQUEST_URL);
HttpURLConnection conn = (HttpURLConnection) imgURL.openConnection();
conn.setRequestMethod("GET");
if (accessToken != null) {
    conn.setRequestProperty("Authorization", "Bearer " + accessToken);
} else {
    conn.setRequestProperty("Authorization", "Client-ID " + CLIENT_ID);
}
BufferedReader bin = null;
bin = new BufferedReader(new InputStreamReader(conn.getInputStream()));
Was it helpful?

Solution

First, the example is using Imgur API v2 which is old and unsupported. You should be using API v3.

Also note that:

For public read-only and anonymous resources, such as getting image info, looking up user comments, etc. all you need to do is send an authorization header with your client_id in your requests.

from docs at https://api.imgur.com/oauth2 -- so you don't really need OAuth for what you're doing.

There is some example Imgur API code that might help you, listed at https://api.imgur.com/ -- the Android example might be more relevant to you, since it uses Java, but unsurprisingly it comes with all the overhead of an Android project, compared with a plain Java application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top