匿名上传文件对象Imgur API(JSON)提供身份验证错误401上传文件、身份验证、对象、错误

2023-09-06 04:18:44 作者:遇我如初见

我创建了一个类 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.

下面是JSON的截图,我从Imgur获得:

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

我抬头错误状态401 api.imgur.com,它说,我需要使用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(串)没有在我的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的)

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);
}

什么是造成问题的原因?

What's causing the problem?

推荐答案

从api.imgur.com文件,重点是我的:

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

这个API要求每个客户端使用OAuth 2认证。意即   你必须注册应用程序,并生成一个access_ code   如果你想作为一个用户登录。

java小白训练营day05 API API 字符串工具类 身份证解析

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);
 
精彩推荐
图片推荐