Anonymous Uploading File object to Imgur API (JSON) gives Authentication Error 401
Asked Answered
P

1

5

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.

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

Imgur Screenshot

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

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.

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?

Pocketful answered 19/5, 2013 at 20:58 Comment(0)
D
11

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

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.

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.

Authorization: Client-ID YOUR_CLIENT_ID

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);
Datura answered 19/5, 2013 at 22:37 Comment(2)
Thanks so much! It worked! Note that the exact code is httpPost.setHeader("Authorization", "Client-ID "+ API_key); based on Imgur's API documentation.Pocketful
For all JS'ian: xhr.setRequestHeader('Authorization', `Client-ID ${API_key}`) is the equivalent. @ me for further inquiry ;)Syndesmosis

© 2022 - 2024 — McMap. All rights reserved.