通过 oAuth2 与 jQuery/AJAX for Box.com 连接 [英] 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/#文件夹创建新文件夹
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 :您选择的任意字符串包含在对您的申请的回复中.盒子建议你使用防伪状态令牌来防止对用户的 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 与 jQuery/AJAX for Box.com 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!