Anonymous loading of File object in Imgur API (JSON) gives authentication error 401

I created the UploadToImgurTask class as AsyncTask, which takes one parameter of the file path, creates and installs MultiPartEntity, and then uses the Apache HttpClient to load the image with the specified object. Imgur's JSON response is stored in a JSONObject, the contents of which I display in LogCat for my own understanding.

Here is a screenshot of the JSON I get from Imgur:

Imgur Screenshot

I was looking for Status 401 error on api.imgur.com and it says that I need to authenticate using OAuth despite the fact that Imgur very clearly stated that applications do not need to use OAuth if the images are downloaded anonymously (this is what I'm 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; } } 

After doInBackground returns a link to the downloaded image on onPostExecute, I want to copy it to the system clipboard, but Eclipse continues to say that getSystemService (String) is not defined in my ASyncTask class.

There is no legal way to return a (String) link back to the main thread, so I have to do everything I have to do in onPostExecute, in 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 causes the problem?

+4
source share
1 answer

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

The API requires that each client use OAuth 2 authentication. This means you will need to register your application and create access_code if you want to log in as a user.

For publicly accessible, public and anonymous resources, such as retrieving image information, viewing user comments, etc., all you have to do is send an authorization header with your client_id in your requests . This also works if you want to anonymously upload images (without an account-linked image), or if you want to create an anonymous album. This allows us to find out which application is accessing the API.

Authorization: Customer ID YOUR_CLIENT_ID

It’s quite clear that you need to add an authorization header to your request for it to work. With Apache HttpClient, it is so simple:

 httpPost.setHeader("Authorization", yourClientId); 
+10
source

Source: https://habr.com/ru/post/1481663/


All Articles