通过oAuth2与Box.com的jQuery/AJAX连接 [英] Connecting via oAuth2 with jQuery/AJAX for Box.com
问题描述
我正在尝试与Box.com的API一起开发一个允许创建文件夹的快速应用程序.我在连接到他们的API时遇到了麻烦,并且对oAUTH2,API以及其他方面还很陌生.我尝试遵循以下指南:
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
Box.com文档说
The Box.com documentation says
response_type:端点是否返回授权码.为了 Web应用程序,应使用代码值.
response_type: Whether the endpoint returns an authorization code. For web applications, a value of code should be used.
client_id:您在初始设置中获得的client_id.
client_id : The client_id you obtained in the Initial Setup.
redirect_uri:HTTPS URI或自定义URL方案,其中响应 将被重定向.如果重定向URI已注册,则为可选 已装箱.
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:您选择的任意字符串将是 包含在对您的应用程序的回复中. Box建议您 使用防伪状态令牌来防止CSRF攻击您的用户
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
因此,示例GET请求可能如下所示:
A sample GET request could therefore look like:
获取 https://www.box.com/api/oauth2/authorize?response_type=code&client_id=MY_CLIENT_ID&state=security_token%3DKnhMJatFipTAnM0nHlZA
GET https: //www.box.com/api/oauth2/authorize?response_type=code&client_id=MY_CLIENT_ID&state=security_token%3DKnhMJatFipTAnM0nHlZA
我有一个开发人员帐户,这是我无法使用的基本jQuery..
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.
推荐答案
您可以使用Axios代替ajax
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>
需要此脚本后,您可以编写自己的javascript函数,它将起作用.
After requiring this script you can write your own javascript function and it will work.
clientId:传递动态客户端ID. 秘密:传递动态秘密.
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}`
}
});
}
对我来说很好.
这篇关于通过oAuth2与Box.com的jQuery/AJAX连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!