我正在尝试使用Imgur API创建图像上传模块 我在注册后获得了客户端ID和客户端密钥。当涉及到实现和测试时,它会失败并在logcat中给出以下响应
Logcat响应
{"data":{"error":"We're really sorry, but
anonymous uploading in your country has
been disabled. Please <a href=\"\/register\">register
for an account<\/a> and try uploading again.","request":"\/3\/upload.json","method":"POST"}
,"success":false,"status":400}
以下是我的代码
public String uploadToImgur(File uploadFile) {
DefaultHttpClient defaulthttpclient;
HttpPost httppost;
MultipartEntity multipartentity;
String path = uploadFile.getAbsolutePath().toString();
String s;
defaulthttpclient = new DefaultHttpClient();
String targetURL = "https://api.imgur.com/3/upload.json";
String apikey = "client_secret";
httppost = new HttpPost(targetURL);
httppost.setHeader("User-Agent", USER_AGENT);
httppost.addHeader("Authorization", "Client-ID {client)_id}");
multipartentity = new MultipartEntity();
s = path.substring(1 + path.lastIndexOf("."));
if (s.lastIndexOf("jpg") >= 0)
{
s = "jpeg";
}
try
{
multipartentity.addPart("image", new FileBody(new File(path), (new StringBuilder("image/")).append(s).toString()));
multipartentity.addPart("key", new StringBody(apikey));
httppost.setEntity(multipartentity);
String s1 = EntityUtils.toString(defaulthttpclient.execute(httppost).getEntity());
Log.d("outpur" , s1);
if (s1.lastIndexOf("<original>") >= 0 && s1.indexOf("</original>") >= 0)
{
return (new StringBuilder("[img]")).append(s1.substring(10 + s1.lastIndexOf("<original>"), s1.indexOf("</original>"))).append("[/img]").toString();
}
}
catch (Exception exception)
{
return "ERRor";
}
return "Error";
}
请您告诉我增强上传模块的更好方法是什么?
答案 0 :(得分:0)
注册和发送客户端ID不足以进行非匿名上传。该文档告诉您使用oAuth并获取需要为此类请求传递的令牌。
<强>认证 API要求每个客户端使用OAuth 2身份验证。这意味着您必须注册您的应用程序,并在您想以用户身份登录时生成access_code。 对于公共只读和匿名资源,例如获取图像信息,查找用户评论等,您只需在请求中发送带有client_id的授权标头即可。如果您想要匿名上传图像(图像不附加到帐户),或者您想要创建匿名相册,这也适用。这让我们知道哪个应用程序正在访问API。 授权:Client-ID YOUR_CLIENT_ID 要访问用户的帐户,请访问文档的OAuth2部分