java - Passing username to a HttpGet request -
i need access api works this:
curl https://api.com/ratings/v1/ -u [your token here]:
the token username should passed httpget
request. trying same in following way using java:
credentialsprovider credentialsprovider = new basiccredentialsprovider(); credentialsprovider.setcredentials(authscope.any, new usernamepasswordcredentials("usrname", "passwrd")); httphost proxy = new httphost("proxy.com", 8080, "http"); httpclient httpclient = httpclients.custom().setproxy(proxy).setdefaultcredentialsprovider(credentialsprovider).build(); httpget toesget = new httpget("https://api.com/ratings/v1/"); toesget.setheader("accept", "application/json"); toesget.addheader("username", "[your token here]"); try { httpresponse toes = httpclient.execute(toesget); system.out.println(toes.getstatusline()); system.out.println(toes.getentity().tostring()); } catch (exception e) { e.printstacktrace(); }
i behind proxy, creating httphost
proxy details, setting proxy httpclient
object , passing credentials proxy authentication using credentialsprovider
in following lines of code:
httphost proxy = new httphost("proxy.com", 8080, "http"); httpclient httpclient = httpclients.custom().setproxy(proxy).setdefaultcredentialsprovider(credentialsprovider).build();
i passing username
httpget
adding header
this:
toesget.addheader("username", "[your token here]");
when run code, response: http/1.1 401 unauthorized
this indicates not passing username
httpget
request in right way(or mean else?). what's right way of passing username request?
any appreciated, thank you!
note: usrname
, passwrd
set in credentialsprovider proxy authentication. have nothing httpget request itself. token need pass different usrname
provided in credentials.
i haven’t used apache httpcomponents, understanding have set credentials specific hosts:
credentialsprovider credentialsprovider = new basiccredentialsprovider(); httphost proxy = new httphost("proxy.com", 8080, "http"); credentialsprovider.setcredentials(new authscope(proxy), new usernamepasswordcredentials("usrname", "passwrd")); credentialsprovider.setcredentials(new authscope("api.com", authscope.any_port), new usernamepasswordcredentials("apiuser", "apipassword"));
note: not type "apiuser" or "apipassword" in code. showing placeholders. replace them correct user , password accessing api.com. (i pointing out because, based on code in question, i’m not sure if understood not supposed use literal string "[your token here]"
.)
Comments
Post a Comment