【发布时间】:2013-11-20 18:05:36
【问题描述】:
所以我正在尝试获取各种图像的信息,为此我将通过 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)
其中
我已检查我的客户端 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()));
【问题讨论】:
-
首先,该示例使用的是旧的且不受支持的 Imgur API v2。您应该使用 API v3。另请注意,“对于公共只读和匿名资源,例如获取图像信息、查找用户 cmets 等,您需要做的就是在请求中发送带有您的 client_id 的授权标头。”来自api.imgur.com/oauth2 的文档——所以你真的不需要 OAuth 来做你正在做的事情。
-
也许您应该将此作为答案发布,因为它回答了我的问题?你能给我一个链接或一些我可以工作的课程吗?我对这个 HTTP 请求的东西真的很陌生。
标签: java api oauth scribe imgur