java - How to authenticate to Mendeley using OAuth2 -
iam trying authenticate mendeley using java , library net.oauth. goal retrieve readership data mendeley add them our database of academic documents.
unfortunatly getting 401 , following exception:
net.oauth.oauthproblemexception @ net.oauth.client.oauthclient.invoke(oauthclient.java:246) @ net.oauth.client.oauthclient.invoke(oauthclient.java:143) @ net.oauth.client.oauthclient.getrequesttoken(oauthclient.java:101) @ net.oauth.client.oauthclient.getrequesttoken(oauthclient.java:77) @ net.oauth.client.oauthclient.getrequesttoken(oauthclient.java:116) @ org.mrdlib.mendeleycrawler.mendeleyconnection.defaultclient(mendeleyconnection.java:82) @ org.mrdlib.mendeleycrawler.mendeleyconnection.getreadership(mendeleyconnection.java:124) @ org.mrdlib.mendeleycrawler.mendeleyconnection.main(mendeleyconnection.java:190) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ org.eclipse.jdt.internal.jarinjarloader.jarrsrcloader.main(jarrsrcloader.java:58)
i use following code:
public class mendeleyconnection { private oauthaccessor client; private string access_token; private string request_token; private dbconnection con; public mendeleyconnection() { con = new dbconnection(); } public string converttoaccesstoken(string request_token) { arraylist<map.entry<string, string>> params = new arraylist<map.entry<string, string>>(); oauthclient oclient = new oauthclient(new httpclient4()); oauthaccessor accessor = client; params.add(new oauth.parameter("oauth_token", request_token)); try { oauthmessage omessage = oclient.invoke(accessor, "post", accessor.consumer.serviceprovider.accesstokenurl, params); return omessage.getparameter("oauth_token"); } catch (oauthproblemexception e) { e.printstacktrace(); return ""; } catch (exception ioe) { ioe.printstacktrace(); return ""; } } public oauthaccessor defaultclient() { string callbackurl = "some fallback url"; string consumerkey = "the id of mendeley application"; string consumersecret = "a generated secret"; string requrl = "https://www.mendeley.com/oauth/request_token/"; string authzurl = "https://api-oauth2.mendeley.com/oauth/authorize/"; string accessurl = "https://www.mendeley.com/oauth/access_token/"; oauthserviceprovider provider = new oauthserviceprovider(requrl, authzurl, accessurl); oauthconsumer consumer = new oauthconsumer(callbackurl, consumerkey, consumersecret, provider); oauthaccessor accessor = new oauthaccessor(consumer); oauthclient oaclient = new oauthclient(new httpclient4()); try { oaclient.getrequesttoken(accessor); request_token = accessor.requesttoken; } catch (oauthproblemexception e) { e.printstacktrace(); system.out.println(e.gethttpstatuscode()); } catch (exception e) { e.printstacktrace(); } return accessor; } public hashmap<string, readership> getreadership() { hashmap<string, readership> map = new hashmap<string, readership>(); list<string> documenttitles = new arraylist<>(); readership readership = null; string mendeleyid = null; int score = 0; httppost httppost = new httppost(); url url = null; string nullfragment = null; jsonobject jsonobject = null; documenttitles = con.getalldocumenttitles(); (int = 0; < documenttitles.size(); i++) { string current = documenttitles.get(i); httpclient httpclient = httpclientbuilder.create().build(); string urlstring = "https://api.mendeley.com/catalog?title=" + current; client = defaultclient(); access_token = converttoaccesstoken(client.requesttoken); try { url = new url(urlstring); uri uri = new uri(url.getprotocol(), url.gethost(), url.getpath(), url.getquery(), nullfragment); httppost = new httppost(uri); httppost.addheader("authorization", "bearer " + client.requesttoken); list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("action", "getjson")); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); string data = entityutils.tostring(response.getentity()); jsonobject = (jsonobject) jsonvalue.parse(data); mendeleyid = (string) jsonobject.get("id"); score = (integer) jsonobject.get("score"); } catch (exception e) { e.printstacktrace(); } [...] } return map; } public static void main(string[] args) { mendeleyconnection mcon = new mendeleyconnection(); mcon.getreadership(); } }
the exception thrown @
oaclient.getrequesttoken(accessor);
since have no experience in topic of http requests , authentification, appreciate help. have read guides mendeley und examples find on internet. used request, didn't worked, too. changed urls mendeley (since in documentation have different ones, don't work). tried different examples. tried api google, pure overkill , couldn't put example together. guessing url might still wrong, since found exact example of method "defaultclient" several times. or maybe there change oauth2?
thanks help!
you're urls incorrect start.
string requrl = "https://www.mendeley.com/oauth/request_token/";
string authzurl = "https://api-oauth2.mendeley.com/oauth/authorize/";
string accessurl = "https://www.mendeley.com/oauth/access_token/";
here documentation on authorisation code flow our developer portal might find useful - http://dev.mendeley.com/reference/topics/authorization_auth_code.html
Comments
Post a Comment