问题
I'm trying to work with Box.com's API to develop a quick app that allows for Folder creation. I am having trouble connecting to their API and am fairly new to oAUTH2, API's, and whatnot. I've tried to follow these guides:
http://developers.box.com/oauth/
http://developers.box.com/docs/#folders-create-a-new-folder
The Box.com documentation says
response_type: Whether the endpoint returns an authorization code. For web applications, a value of code should be used.
client_id : The client_id you obtained in the Initial Setup.
redirect_uri :An HTTPS URI or custom URL scheme where the response will be redirected. Optional if the redirect URI is registered with Box already.
state : An arbitrary string of your choosing that will be included in the response to your application. Box recommends that you use an anti-forgery state token to prevent CSRF attacks to your users
A sample GET request could therefore look like:
GET https: //www.box.com/api/oauth2/authorize?response_type=code&client_id=MY_CLIENT_ID&state=security_token%3DKnhMJatFipTAnM0nHlZA
I have a dev account with them and here is my basic jquery that is not working..
$.ajax({
//The URL to process the request
url : 'https://www.box.com/api/oauth2/authorize',
type : 'GET',
data : {
response_type : 'code',
client_id : 'm025a55gtov17txux1v2vbzjjhph2b6n'
},
success: function( resp ) {
console.log( resp.people );
},
error: function( req, status, err ) {
console.log( 'something went wrong', status, err );}
});
Can anyone point me in the direction on how to do this? I'm stumped.
回答1:
I did find a way to connect to their API and get a token, but now I am getting a CORS error when I try and send a POST request to their server to create a folder (the main goal of my app) for anyone interested.. here is how I trade off the code for the token
authorizeUser = function(){
var results = $.ajax({
// The URL to process the request
url : 'https://www.box.com/api/oauth2/token',
type : 'POST',
data : {
grant_type : 'authorization_code',
code : data.boxAuthorizationCode,
client_id : data.clientId,
client_secret : data.clientSecret
},
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Bearer $token")
},
dataType: "json",
success: function(response) {
//console.log(response);
console.log(response.access_token);
data.access_token = response.access_token;
tokenGranted();
}
});
return results.responseText;
},
回答2:
You can use Axios instead of ajax
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qs/6.9.4/qs.js"></script>
After requiring this script you can write your own javascript function and it will work.
clientId: Pass dynamic client id. secret: Pass dynamic secret.
async function runAuthQuery(params) {
const config = {
url: '{WebURL}',
method: 'post',
data: Qs.stringify({
grant_type: 'client_credentials',
client_id: clientId,
client_secret: secret,
})
};
const bearerToken = await axios(config);
const getPlayableUrl = await axios.get(`{WebURL}`,
{
"headers": {
"content-type": "application/x-www-form-urlencoded",
"authorization": `Bearer ${bearerToken.data.access_token}`
}
});
}
It is working fine for me.
来源:https://stackoverflow.com/questions/23482705/connecting-via-oauth2-with-jquery-ajax-for-box-com