android - Cloudant/CouchDB Permissions on Key/Pass not working -
i having problem getting information cloudant.com using key/pass read permissions (or permissions). receiving 500 error way setup user. however, working fine, have left myself open hacking, have have database open read everyone. , know can problem. wondering if has insight why problem happens. btw, have tried in both android , ios applications. current question using examples android app.
here criteria singleton utopia:
private static string _remote_db_protocal = "https"; private static string _remote_db_key = "mykey"; private static string _remote_db_pass = "mypass"; private static string _remote_db_account = "myaccount"; private static string _remote_db_dbname = "mydbname"; public static string remote_json_db_url = _remote_db_protocal+"://"+ _remote_db_key+":"+_remote_db_pass+"@"+ _remote_db_account+".cloudant.com/"+_remote_db_dbname;
here information sending url string response:
private static string dbbaseurl = utopia.remote_json_db_url; .... string dbqueryurl = "/_design/app/_view/toys"; url finalurl; try { finalurl = new url(dbbaseurl + dbqueryurl); log.i(tag, "getting url: "+finalurl.tostring()); response = webstuff.geturlstringresponse(finalurl); ....
and lastly, here geturlstringresponse() function:
/* * information url */ public static string geturlstringresponse(url url) { string response = ""; try { urlconnection conn = url.openconnection(); bufferedinputstream bin = new bufferedinputstream(conn.getinputstream()); byte[] contentbytes = new byte[1024]; int bytesread = 0; stringbuffer responsebuffer = new stringbuffer(); while ((bytesread = bin.read(contentbytes)) != -1) { response = new string(contentbytes, 0, bytesread); responsebuffer.append(response); } return responsebuffer.tostring(); } catch (exception e) { // todo: handle exception log.e(tag, "geturlstringresponse error follows"); log.e(tag, e.tostring()); } return response; }
so of above, hope can answer problem. must note, take exact url query gets spit out in log somewhere, or recreate remote_json_db_url variable , values of i'm looking for, , past browser, worked fine (yes, being logged off of account).
edit: correction, when paste browser, asks login credentials , using key/pass makes work. asked cloudant developer pose question stackoverflow because unsure of happening (they had whole app key/pass credentials , all).
ok, seemed have figured out. went down few different paths after getting will's answer yesterday, alas of them didn't me close. of them change error code 500 401, changing desired use of bufferedinputstream. regardless, if want use bufferedinputstream, urlconnection, , include authentication things such cloudant or whatever case may be, need use setrequestproperty. now, said, tried route few different ways , adding user , password in , doing base64 switch after connection has been made , url passed function didn't work @ all. so, if able pass key/pass in url had, following work you... in theory (it worked me anyhow).
if (url.getuserinfo() != null) { string basicauth = "basic " + new string(base64.encode(url.getuserinfo().getbytes(), 0)); conn.setrequestproperty("authorization", basicauth); }
and of course, here implemented in function can full effect.
public static string geturlstringresponse(url url) { string response = ""; try { urlconnection conn = url.openconnection(); // magic entry if (url.getuserinfo() != null) { string basicauth = "basic " + new string(base64.encode(url.getuserinfo().getbytes(), 0)); conn.setrequestproperty("authorization", basicauth); } // end of magic change bufferedinputstream bin = new bufferedinputstream(conn.getinputstream()); byte[] contentbytes = new byte[1024]; int bytesread = 0; stringbuffer responsebuffer = new stringbuffer(); while ((bytesread = bin.read(contentbytes)) != -1) { response = new string(contentbytes, 0, bytesread); responsebuffer.append(response); } return responsebuffer.tostring(); } catch (exception e) { // todo: handle exception log.e(tag, "geturlstringresponse error follows"); log.e(tag, e.tostring()); } return response; }
i hope helps of out.
Comments
Post a Comment