java - Anonymous Uploading File object to Imgur API (JSON) gives Authentication Error 401 -


i've created class uploadtoimgurtask asynctask takes single file path parameter, creates , sets multipartentity, , uses apache httpclient upload image said entity. json response imgur saved in jsonobject, contents of display in logcat own understanding.

here's screenshot of json receive imgur:

imgur screenshot

i looked error status 401 on api.imgur.com , says need authenticate using oauth despite fact imgur has stated apps not need use oauth if images being uploaded anonymously (which 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 own understanding               return true;         } catch (exception e) {             e.printstacktrace();         }         return false;     } } 

after doinbackground returns link uploaded image onpostexecute, want copy system clipboard, eclipse keeps saying getsystemservice(string) isn't defined within asynctask class.

there no legit way return link (a string) main thread, have whatever have 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 copy");     clipboard.setprimaryclip(clip); } 

what's causing problem?

from api.imgur.com documentation, emphasis mine:

the api requires each client use oauth 2 authentication. means you'll have register application, , generate access_code if you'd log in user.

for public read-only , anonymous resources, such getting image info, looking user comments, etc. need send authorization header client_id in requests. works if you'd upload images anonymously (without image being tied account), or if you'd create anonymous album. lets know application accessing api.

authorization: client-id your_client_id

its quite clear need add authorization header request in order work. apache httpclient simple this:

httppost.setheader("authorization", yourclientid); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -