1
votes

Iam currently trying to authenticate with Mendeley using Java and the library net.oauth. My goal is to retrieve the readership data from Mendeley to add them to our database of academic documents.

Unfortunatly I am currently getting a 401 and the following Exception:

net.oauth.OAuthProblemException at net.oauth.client.OAuthClient.invoke(OAuthClient.java:246) at net.oauth.client.OAuthClient.invoke(OAuthClient.java:143) at net.oauth.client.OAuthClient.getRequestToken(OAuthClient.java:101) at net.oauth.client.OAuthClient.getRequestToken(OAuthClient.java:77) at net.oauth.client.OAuthClient.getRequestToken(OAuthClient.java:116) at org.mrdlib.mendeleyCrawler.mendeleyConnection.defaultClient(mendeleyConnection.java:82) at org.mrdlib.mendeleyCrawler.mendeleyConnection.getReadership(mendeleyConnection.java:124) at org.mrdlib.mendeleyCrawler.mendeleyConnection.main(mendeleyConnection.java:190) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

I use the 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 the 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();

    for (int i = 0; i < 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 is thrown at

oaclient.getRequestToken(accessor);

Since I have no experience in the topic of Http Requests and Authentification, I would appreciate some help. I have read the guides from Mendeley und all examples I could find on the Internet. I also used a Get request, but this didn't worked, too. I changed the urls from Mendeley (since in the documentation they have different ones, which don't work). I tried different examples. I even tried the API from Google, but this was a pure Overkill and i couldn't even put a example together. I am currently guessing that my url might still be wrong, since I found exact the example of the method "defaultClient" several times. Or maybe there was a change with OAuth2?

Thanks for the help!

1

1 Answers

0
votes

You're URLs are incorrect for a 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 is the documentation on the authorisation code flow from our developer portal that you might find useful - http://dev.mendeley.com/reference/topics/authorization_auth_code.html