相关疑难解决方法(0)

匿名上传File对象到Imgur API(JSON)给出了身份验证错误401

我创建了UploadToImgurTask一个AsyncTask 类,它接受单个文件路径参数,创建并设置MultiPartEntity,然后使用Apache HttpClient上传带有所述实体的图像.来自Imgur的JSON响应保存在JSONObject中,我在LogCat中显示的内容供我自己理解.

这是我从Imgur收到的JSON的屏幕截图:

Imgur截图

我在api.imgur.com上查找了错误状态401,它说我需要使用OAuth进行身份验证,尽管事实上Imgur已经明确表示如果图像是匿名上传的,应用程序不需要使用OAuth(这就是我我现在正在做

class UploadToImgurTask extends AsyncTask<String, Void, Boolean> {
    String upload_to;

    @Override
    protected Boolean doInBackground(String... params) {
        final String upload_to = "https://api.imgur.com/3/upload.json";
        final String API_key = "API_KEY";
        final String TAG = "Awais";

        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(upload_to);

        try {
            final MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            entity.addPart("image", new FileBody(new File(params[0])));
            entity.addPart("key", new StringBody(API_key));

            httpPost.setEntity(entity);

            final HttpResponse response = httpClient.execute(httpPost,
                    localContext);

            final String response_string …
Run Code Online (Sandbox Code Playgroud)

java api android json imgur

4
推荐指数
1
解决办法
5551
查看次数

标签 统计

android ×1

api ×1

imgur ×1

java ×1

json ×1