3

I hope you won't mind my posting a question, because I'm not a programmer and need a "for dummies" explanation. Although I can use basic JavaScript, I've never used an API.

This is what I want to do:

I've made an HTML page on my hard-drive (which I ultimately want to package with something like PhoneGap into a mobile app so I can share with my colleagues).

Part of my page, I want to be populated with the contents of a folder of annotated citations that I have in the web-based Mendeley reference manager application.

At present, I'm populating my page with a JSON file (which I exported from Mendeley and re-formatted - just to get my citation formatting code right - that part is fine, it looks great).

But I'd like my page to populate not with a previously exported file, but with the up-to-date contents of my Mendeley folder automatically downloaded into my page when my app is opened.

I've laboured over the Mendeley api "documentation", checked out books, YouTube tutorials, searched the web - all the examples of using an api to download data that I can find, don't help me figure out how to use the Mendeley api. I can't even understand from the 'examples' in the Mendeley SDK!

This is all I've been able to understand (or perhaps mis-understand!) so far:

  1. Register with Mendeley and get a 'client-id' and a 'secret': I've done that
  2. Decide what kind of authentication flow I want: this will be 'implicit flow' since I need to access Mendeley directly from the browser. I understand 'implicit flow' doesn't need the 'secret', just the 'client-id'.
  3. Use the Mendeley JavaScript SDK: I'm using the 'standalone' version - I'm referencing it with a script tag in my page, as described in the Readme SDK file. I've also copied and pasted a required snippet of code for implicit flow into a file named 'oauth-config.js', as described in the SDK examples file: oauth-config.implicit-grant.js.dist

But now what?

  • How exactly do I use the SDK to get my data? I can find no directions.
  • Assuming I can get my data from Mendeley, how does my script which processes the JSON for display in my page access the response data?
halfer
  • 19,824
  • 17
  • 99
  • 186
JaneB
  • 31
  • 2

1 Answers1

1

Let me give you an example of how to use the Mendeley JS API. Here's my minimalistic implementation of login with implicit flow and retrieving all documents in user personal library.

var sdk = require('@mendeley/api');
var api = sdk({
  authFlow: sdk.Auth.implicitGrantFlow({
    clientId: <YOUR_CLIENT_ID_GOES_HERE>
  })
})

api.documents.list().then(function(docs) {
    console.log('Success!');
    console.log(docs);
}).catch(function(response) {
    console.log('Failed!');
    console.log('Status:', response.status);
});

You can extend this to access any API provided by the SDK.

However due to the way the authentication works, you need to host this script on your website on the exact same URL as the one you have provided in the "redirect URL" when you registered your client. Otherwise you will get an error message Redirection URI does not match the one registered for this application.

For the second part of the question, I believe you are asking how to retrieve all documents within a certain Mendeley folder (correct me if I am understanding this incorrectly). Say you have a folder with a folder id aaaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee. Then you can modify the above example to retrieve all documents in that folder like so:

var sdk = require('@mendeley/api');
var api = sdk({
  authFlow: sdk.Auth.implicitGrantFlow({
    clientId: <YOUR_CLIENT_ID_GOES_HERE>
  })
})

api.documents.list({
    folderId: 'aaaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
}).then(function(docIds) {
    console.log('Success!');
    console.log(docIds);
}).catch(function(response) {
    console.log('Failed!');
    console.log('Status:', response.status);
});

In this case the response will contain the document ids so you will need to call the api.documents.retrieve(<DOCUMENT_ID>) API to get the document details.

  • Unfortunately, I don't understand how this runs. When you create the variable `api` and set the `AuthFlow` the browser will be redirected to the Mendeley login page and redirected back. Should you extract the token? The call to `api.documents.list` fails on my side, because there's no token. – cezar Apr 01 '21 at 12:38