0

I am trying to use the Mendeley API to download a file attached to a resource (document). I am following the developer documentation here: https://dev.mendeley.com/methods/#downloading-a-file It looks like it should be fairly straight forward. I have the Oauth flow figured out and I am able to pull all of my documents from Mendeley library. I am also able to get the attached file id for a given document.

It is when I try to use that ID to retrieve the file (a PDF in this case) that it fails. I have copied the parameters to postman and it works fine there, but when I try to do it in my Ruby-on-rails app using JavaScript it fails.

I am a novice when it comes to JavaScript so I may very well be missing something obvious.

Here is the JavaScript code snippet:

downloadFile = (json) => {
        this.getAuthCode();
        this.provideToken = (token) => {
            const access_token = token;
            var myHeaders = new Headers();
            myHeaders.append("Authorization", `Bearer ${access_token}`);
            var requestOptions = {
                method: 'GET',
                headers: myHeaders,
            };
            fetch(`https://api.mendeley.com/files/${json[0].id}`, requestOptions).then(resp => {
                console.log(resp)
            })
        }
    }

The JSON argument is an array of files associated with a document. It seems to work ok until the Mendeley server redirects to the AWS S3 storage URL for the file in question. Here is the Chrome developer console output:

Response {type: "cors", url: "https://mendeley-files.s3.amazonaws.com/31/9f/319f…09d6b8443d1e734fad70c551e37a0a3497b42c69fd0796c91", redirected: true, status: 400, ok: false, …}
body: (...)
bodyUsed: false
headers: Headers
__proto__: Headers
ok: false
redirected: true
status: 400
statusText: "Bad Request"
type: "cors"
url: "https://mendeley-files.s3.amazonaws.com/31/9f/319fe16b678cc65092ef5aa732b3e4c137f272f3?response-content-disposition=attachment%3Bfilename%3D%222003-Psychometric_properties_of_student_ratings_of_instruction_in_online_and_on-campus_courses.pdf%22&response-content-type=application%2Fpdf&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEOz%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJIMEYCIQCP0qEL1EJeGKIBVmxX2FaPIZftcmqiW%2FzgDY5ysRs25AIhAMM6q3YzWSubSnuuXOs9gi8voOZ%2BRrkQS343IkihJka1KvYDCDUQAhoMMTA4MTY2MTk0NTA1Igx2zsOuUuv1h6V8cw0q0wMNdVy5izfb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host&X-Amz-Expires=600&X-Amz-Credential=ASIARSLZVEVE5V2CTSJF%2F20200511%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=96b2a6673ab742909d6b8443d1e734fad70c551e37a0a3497b42c69fd0796c91"
__proto__: Response
arrayBuffer: ƒ arrayBuffer()
blob: ƒ blob()
body: (...)
bodyUsed: (...)
clone: ƒ clone()
formData: ƒ formData()
headers: (...)
json: ƒ json()
ok: (...)
redirected: (...)
status: (...)
statusText: (...)
text: ƒ text()
type: (...)
url: (...)
constructor: ƒ Response()
Symbol(Symbol.toStringTag): "Response"
get body: ƒ body()
get bodyUsed: ƒ bodyUsed()
get headers: ƒ headers()
get ok: ƒ ok()
get redirected: ƒ redirected()
get status: ƒ status()
get statusText: ƒ statusText()
get type: ƒ type()
get url: ƒ url()
__proto__: Object

It clearly finds the correct file but fails to download it. When I run it in Postman it downloads the file.

I have tried adding various headers that show up in Postman as 'hidden' but I get basically the same response. Any thoughts or direction on this would be greatly appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Xswede
  • 45
  • 1
  • 7

1 Answers1

1

Reference documentation: https://dev.mendeley.com/code/core_quick_start_guides.html#file_download

This is working for me:

async function getFile(fileId) {
  const url = `https://api.mendeley.com/files/${fileId}`;
  const res = await superagent.get(url).auth(accessToken, { type: 'bearer' });
  const [fileUrl] = res.redirects;
  return fileUrl;
}
user2386601
  • 101
  • 6