For fun, I'm making a cataloging web app using asp.net core 2.1 MVC and javascript. I don't need a frontend framework, so I don't want to bring in the whole NodeJS npm situation into this project. I'm trying to access https://api.dropboxapi.com/2/files/get_metadata using fetchAPI and Chrome and I am getting this error: 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
Their is a similar question on this forum, and the answer was to use the Dropbox Javascript library. I thought this API was like any other restful api where all you needed was to send http requests to endpoints. Is the library required?
Here's all my javascript code. I am using the chooser to get the id of a folder, then I need to make a call to get_metadata to get the path (why doesn't chooser return the path itself? I guess the folder names are too secret.)
var options = {
// Required. Called when a user selects an item in the Chooser.
success: function (files) {
//alert("Here's the file link: " + files[0].link);
console.log('files[0]:', files[0]);
dbxGetPathFromId(files[0]);
},
// Optional. Called when the user closes the dialog without selecting a file
// and does not include any parameters.
cancel: function () {
console.log('Canceled');
},
// Optional. "preview" (default) is a preview link to the document for sharing,
// "direct" is an expiring link to download the contents of the file. For more
// information about link types, see Link types below.
//linkType: "preview", // or "direct"
// Optional. A value of false (default) limits selection to a single file, while
// true enables multiple file selection.
multiselect: false, // or true
// Optional. This is a list of file extensions. If specified, the user will
// only be able to select files with these extensions. You may also specify
// file types, such as "video" or "images" in the list. For more information,
// see File types below. By default, all extensions are allowed.
extensions: ['.NoEmptyFiles'],
// Optional. A value of false (default) limits selection to files,
// while true allows the user to select both folders and files.
// You cannot specify `linkType: "direct"` when using `folderselect: true`.
folderselect: true, // or true
// Optional. A limit on the size of each file that may be selected, in bytes.
// If specified, the user will only be able to select files with size
// less than or equal to this limit.
// For the purposes of this option, folders have size zero.
sizeLimit: 1, // or any positive number
};
var button = Dropbox.createChooseButton(options);
document.getElementById("dropbox-btn").appendChild(button);
function status(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response);
} else {
return Promise.reject(new Error(response.statusText));
}
}
function json(response) {
return response.json();
}
function dbxGetPathFromId(file) {
// do an ajax call to dbx api, print the result
// post to https://api.dropboxapi.com/2/files/get_metadata with auth
// use json
reqData = {
"path": file.id
};
apiURL = 'https://api.dropboxapi.com/2/files/get_metadata';
fetch(apiURL, {
method: 'post',
headers: {
"Content-type": "application/json; charset=UTF-8"
},
body: reqData,
credentials: 'include'
})
.then(json)
.then(function (data) {
console.log('Request succeeded with JSON response', data);
})
.catch(function (error) {
console.log('Request failed', error);
});
}