所以我试图获取各种图像的信息,为此我将通过 Java 使用 Imgur API。
我找到了一个图书馆:https ://github.com/fernandezpablo85/scribe-java ,
但是在尝试 ImgUrTest.java @ https://github.com/fernandezpablo85/scribe-java/blob/master/src/test/java/org/scribe/examples/ImgUrExample.java时,我得到以下堆栈跟踪:
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)
其中 <Client-ID> 是我的客户 ID,可在 ImgUr 的页面上找到。
我检查了我的客户端 ID 和客户端密码是否正确,我尝试在 ImgUr 网站上制作多个应用程序,但都不起作用。
编辑:此代码有效:
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()));