Dropbox shared_link - javascript

I'm writing code to get a link to an image file in Dropbox. However, I get a 400 error.
The error content was "Error in call to API function " sharing / create_shared_link_with_settings \ ": request body: could not decode input as JSON". I think the description method is wrong, but I don't know what's wrong.
<!doctype html>
<html>
<head>
</head>
<body>
<form id="form">
<h3>ファイルを選択してアップロード</h3>
<input type="file" id="file">
</form>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script>
var input = document.getElementById("file");
//ダイアログでファイルが選択された時
input.addEventListener("change", function (evt) {
//ドロップボックスのファイルAPI
json = {
"url": 'https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings',
"raw_url": "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings",
"type": 'post',
"data": {
"{\"path\": \"/hand4.jpg\",\"settings\": {\"audience\": \"public\",\"access\": \"viewer\",\"requested_visibility\": \"public\",\"allow_download\": true}}": ""
},
"headers": {
"Authorization": "Bearer *********",
"Content-Type": "application/json"
},
success: function (data) {
console.log(data);
},
error: function (data) {
console.error(data);
}
}
$.ajax(json)
})
</script>
</body>
</html>

The /2/sharing/create_shared_link_with_settings Dropbox API endpoint is an "RPC" style endpoint, meaning it expects its parameters as JSON in the request body.
The error is indicating that you are not sending valid JSON in the request body.
In your code, you're attempting to set that via the data field in your json dict, which is itself a dict with one element, with the key being the escaped string of your desired parameters, and the value being an empty string. That should instead just be the JSON string of the desired parameters.
For example, you probably meant to do something like this instead:
"data": JSON.stringify({"path": "/hand4.jpg","settings": {"audience": "public","access": "viewer","requested_visibility": "public","allow_download": true}}),

Related

How to receive judgement result from model of Custom Vision with JavaScript

I want to receive judgement result from model of Azure Custom Vision by using JavaScript.
I changed JavaScript code that this site has.
https://southcentralus.dev.cognitive.microsoft.com/docs/services/eb68250e4e954d9bae0c2650db79c653/operations/58acd3c1ef062f0344a42814
But I can't.
What is wrong with my code?
I changed IterationId, application, url, content-Type, Prediction-key, and data.
These part are enclosed with {} in the code below.
<!DOCTYPE html>
<html>
<head>
<title>Human</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js">
</script>
</head>
<body>
<script type="text/javascript">
$(function() {
var params = {
// Request parameters
"iterationId": "{Iteration id that showed in Performance Page}",
"application": "{My Project name of Custom Vision}",
};
$.ajax({
url: "{url that showed in "How to use the Prediction API"}" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/octet-stream");
xhrObj.setRequestHeader("Prediction-key","{my prediction key that showed in "How to use the Prediction API"}");
},
type: "POST",
// Request body
data: "D:\some name\some name\image.jpg",
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
});
</script>
</body>
</html>
Of course, I expected showing "success".
But, the actual output is "error"......
When I changed URL that this site has(https://southcentralus.dev.cognitive.microsoft.com/docs/services/eb68250e4e954d9bae0c2650db79c653/operations/58acd3c1ef062f0344a42814) in my code, I can get Success message.
And, I also write
processData: false,
contentType: false,
in ajax in my code
Change your code to see what is the error that you get back:
(Note the new "error" parameter to the request)
$.ajax({
url: "{url that showed in "How to use the Prediction API"}" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/octet-stream");
xhrObj.setRequestHeader("Prediction-key","{my prediction key that showed in "How to use the Prediction API"}");
},
type: "POST",
// Request body
data: "D:\some name\some name\image.jpg",
error: function(xhr,status,error) {
// >>>>>>>>>>>> CHECK HERE THE ERROR <<<<<<<<<<<<
}
})
.done(function(data) {
alert("success");
})
.fail(function() {
alert("error");
});
Once you have the error, it would be easier to help you.

Ajax call on success not working

I have to convert a encrypted data response back to plain text.
For this I am using a ajax post call to get the data response and than on sucess passing that data response a decrypto function and getting back the response in plain text (this is the idea)
Now, when I created a Ajax post call and than passing that decrypto their on success nothing is happening
I am sure my decrypto is working
Here is working fiddle for that
https://jsfiddle.net/yktup39e/
Now, problem is in the Ajax. I have only once make an Ajax call so I pretty much sure there will problem in that. But I am not getting that.
This is my code-
JS -
$('#action-button').click(function() {
var settings = {
async: true,
crossDomain: true,
url: "http://192.168.168.76:8080/HTMLPortingNewService/GetData?ChartName=widget3LineChart&lob=M&carrier=0&enrollmenttype=0&state=0&agent=0&fromdate=04%2F03%2F2015&todate=05%2F03%2F2015&requestID=499F6BF5E4610454A887AB37AF0814E8",
method: "POST",
headers: {
"cache-control": "no-cache",
"postman-token": "ac20a050-a8c8-6d58-4350-66141d519394",
"content-type": "application/x-www-form-urlencoded"
},
data: {
"username": "aHRtbHVzZXIx",
"password": "SHRtbDIwMTY="
}
}
$.ajax(settings).done(function (response) {
console.log(response);
decrypted = decryptByDES(response,DES);
console.log(decrypted);
});
});
function decryptByDES(cipherTextString, keyString) {
var keyHex = CryptoJS.enc.Utf8.parse(keyString);
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(cipherTextString)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
alert(decrypted);
return decrypted.toString(CryptoJS.enc.Utf8);
}
HTML -
<script src="tripledes.js"></script>
<script src="mode-ecb.js"></script>
<button id="action-button">Click me to load info!</button>
<div id="info"></div>
Can anyone please help me with that??
Try to include jquery in your page.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.js"></script>

Implementing Microsoft's Project Oxford - Emotion API and file upload

I'm looking to be able to implement the Emotion API from Project Oxford on my website. I've currently written the below HTML/JavaScript code which checks an image from a URL and displays the result of said image after having run the Emotion API:
<head>
<title>JSSample</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<body>
<script type="text/javascript">
$(function() {
$.ajax({
url: "https://api.projectoxford.ai/emotion/v1.0/recognize",
beforeSend: function(xhrObj) {
// Request headers
xhrObj.setRequestHeader("Content-Type", "application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", "my-key");
},
type: "POST",
// Request body
data: '{"url": "https://philosophybank.files.wordpress.com/2013/08/happy-people.jpg"}',
})
.done(function(data) {
JSON.stringify(data);
alert(JSON.stringify(data));
//console.log(data);
//alert(data.scores);
})
.fail(function(error) {
console.log(error.getAllResponseHeaders());
alert("fail");
});
});
</script>
This code works fine, however I'm looking to implement this on my website such that people upload images themselves locally from their machine with the use of a browse button as opposed to looking up an image using the link. Any help would be very much appreciated!
I mocked this up using application/octet-stream as the body type which allows you to post a binary object (i.e. the image itself), rather than a url to an image. The Emotion API documentation details how this is a supported content type.
I've continued with use of JQuery as per your original example.
You should be able to copy and paste this entire example into a HTML file, add your Emotion API key where it says my-key and it will work
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<input type="file" id="file" name="filename">
<button id="btn">Click here</button>
<script type="text/javascript">
$('#btn').click(function () {
var file = document.getElementById('file').files[0];
$.ajax({
url: "https://api.projectoxford.ai/emotion/v1.0/recognize",
beforeSend: function(xhrObj) {
// Request headers
xhrObj.setRequestHeader("Content-Type", "application/octet-stream");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", "my-key");
},
type: "POST",
data: file,
processData: false
})
.done(function(data) {
JSON.stringify(data);
alert(JSON.stringify(data));
})
.fail(function(error) {
alert(error.getAllResponseHeaders());
});
});
</script>
</body>
</html>

Error when making Ajax REST API calls

I have this application that I am trying to create.
The application should login (using REST API), and respond with a session id.
I have created a button, which calls the JavaScript function that performs the ajax call, which in turn tries to login.
The results are appended to the div (appscan_results)
Here's is the code
<html>
<head>
<title> AppScan Issues Exporter </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
function login(){
var request = $.ajax({
type:"POST",
url:"myurl",
dataType: "json",
contentType: "application/json",
data:{
"userId": "username",
"password": "password",
"featureKey": "AppScanEnterpriseUser",
"clientVersion": "",
"clientIp": "",
"clientHostName": ""
},
});
request.done(function (jqXHR){
alert('Success');
$("#appscan_results").html(jqXHR.responseText);
});
request.fail(function(jqXHR) {
alert('Failure');
$("#appscan_results").html(jqXHR.responseText);
});
}
</script>
<button onclick="login()">Get Issues</button>
<div id="appscan_login"></div>
<div id="appscan_results"></div>
</body>
</html>
After it runs, I get the following error:
Exception thrown by application class 'org.apache.wink.server.internal.RequestProcessor.handleRequest:195'
javax.servlet.ServletException: org.codehaus.jackson.JsonParseException: Unexpected character ('u' (code 117)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: com.ibm.ws.webcontainer.srt.SRTInputStream#d80cceaa; line: 1, column: 2]
at org.apache.wink.server.internal.RequestProcessor.handleRequest(RequestProcessor.java:195)
at com.ibm.websphere.jaxrs.server.IBMRestServlet.service(IBMRestServlet.java:106)
at [internal classes]
at com.ibm.appscan.server.filters.ClickjackFilter.doFilter(Unknown Source)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:192)
at [internal classes]
What am I missing in the code?
PS: I have tested the REST call using REST API client and I do get a response,
org.codehaus.jackson.JsonParseException
Your servlet/rest controller is not able to parse the Input JSON string and convert it to a Valid Java Object.
Change data to
data:JSON.stringify({
userId: "username",
password: "password",
featureKey: "AppScanEnterpriseUser",
clientVersion: "",
clientIp: "",
clientHostName: ""
},
})

How can I get the results of a JSON POST back

I'm posting this JSON from a form page
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/dojo.js" type="text/javascript"></script>
<script type="text/javascript">
function poster()
{
var dataToPost = {grant_type: "password", username: dojo.byId("username").value, password: dojo.byId("password").value, redirect_uri: "http://localhost/default.html"};
var xhrArgs =
{
url: "https://localhost/api/did/authenticate?client_id=12345",
handleAs: 'json',
postData: dojo.toJson(dataToPost),
headers: { "Content-Type": "application/json; charset=UTF-8", "Accept" : "application/json" },
load: function(data, args)
{
alert("Data = " + data);
},
error: function(error, args)
{
alert("Error! " + error);
}
}
dojo.rawXhrPost(xhrArgs);
}
</script>
But I'm not able to get the JSON results from said POST. How can I get those results? Please help. The data I get on the load function is null
The script at https://localhost/api/did/authenticate needs to print or echo or write or otherwise return the JSON as text as it exits.
Turns out that what I was trying to do is not possible in JavaScript since I'm trying to do it from one domain into another... so the cross-domain implementation is not possible, unless I use an embedded flash object, or a proxy pass in my server. thanks for the help though...

Categories

Resources