匿名上传文件对象到 Imgur API (JSON) 给出身份验证错误 401 [英] Anonymous Uploading File object to Imgur API (JSON) gives Authentication Error 401

查看:17
本文介绍了匿名上传文件对象到 Imgur API (JSON) 给出身份验证错误 401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a class UploadToImgurTask as an AsyncTask that takes a single file path parameter, creates and sets an MultiPartEntity, and then uses Apache HttpClient to upload the image with said entity. The JSON response from Imgur is saved in a JSONObject, the contents of which I display in LogCat for my own understanding.

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

Here's a screenshot of the JSON I receive from Imgur:

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

I looked up error Status 401 on api.imgur.com and it says that I need to authenticate using OAuth despite the fact that Imgur has stated very clearly that apps do not need to use OAuth if images are being uploaded anonymously (which is what I am doing right now).

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 = EntityUtils.toString(response
                    .getEntity());

            final JSONObject json = new JSONObject(response_string);

            Log.d("JSON", json.toString()); //for my own understanding 

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}

<小时>

在 doInBackground 将上传图像的链接返回到 onPostExecute 后,我想将其复制到系统剪贴板,但 Eclipse 一直说 getSystemService(String) 未在我的 ASyncTask 类中定义.


After doInBackground returns the link to the uploaded image to onPostExecute, I want to copy it to the system clipboard, but Eclipse keeps saying getSystemService(String) isn't defined within my ASyncTask class.

没有将链接(字符串)返回到主线程的合法方法,因此我必须在 UploadToImgurTask(扩展 ASyncTask)中的 onPostExecute 中执行我必须执行的任何操作

There is no legit way to return link (a String) back to the main thread, so I have to do whatever I have to do within onPostExecute within UploadToImgurTask (which extends ASyncTask)

    @Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
    ClipData clip = ClipData.newPlainText("label", "Text to copy");
    clipboard.setPrimaryClip(clip);
}

是什么导致了问题?

推荐答案

来自 api.imgur.com 文档,重点是我的:

From the api.imgur.com documentation, emphasis is mine:

API 要求每个客户端使用 OAuth 2 身份验证.这意味着您必须注册您的应用程序,并生成一个 access_code如果您想以用户身份登录.

The API requires each client to use OAuth 2 authentication. This means you'll have to register your application, and generate an access_code if you'd like to log in as a user.

对于公开的只读和匿名资源,例如获取图片信息、查找用户评论等,您只需发送一个请求中带有 client_id 的授权标头.这也是如果您想匿名上传图像(没有图像绑定到一个帐户),或者如果您想创建一个匿名专辑.这让我们知道哪个应用程序正在访问 API.

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. This also works if you'd like to upload images anonymously (without the image being tied to an account), or if you'd like to create an anonymous album. This lets us know which application is accessing the API.

授权:客户 ID YOUR_CLIENT_ID

Authorization: Client-ID YOUR_CLIENT_ID

很明显,您需要向请求添加授权标头才能使其工作.使用 Apache HttpClient 就这么简单:

Its quite clear you need to add an authorization header to your request in order for it to work. With the Apache HttpClient its as simple as this:

httpPost.setHeader("Authorization", yourClientId);

这篇关于匿名上传文件对象到 Imgur API (JSON) 给出身份验证错误 401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆