Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function(api, $, undefined){
- var api = {},
- dom = {},
- data = {
- clientId: "m025a55gtov17txux1v2vbzjjhph2b6n",
- clientSecret: "s7GIpGI4Kt0dE76SlGYzpasBxIShQPDg",
- boxAuthorizationCode: "",
- appState: "authenticate",
- access_token: ""
- },
- setupAppState = function(){
- var authCode = getUrlParameter("code");
- var isAuthCodeDefined = authCode !== null;
- if(isAuthCodeDefined){
- data.boxAuthorizationCode = authCode;
- data.appState = "authorize";
- }
- // console.log(dom.appWrapper.attr("data-state"));
- dom.appWrapper.attr("data-state", data.appState);
- },
- setupDom = function(){
- dom.document = $(document);
- dom.appWrapper = $("#appWrapper");
- // console.log(dom);
- },
- addEventHandlers = function(){
- dom.document.on("click", "#start", function(){
- window.location = "https://api.box.com/oauth2/authorize?client_id=" + data.clientId + "&response_type=code";
- });
- dom.document.on("click", "#continue", function(){
- authorizeUser();
- });
- // dom.document.on("click", "#submit", function(){
- // createFolders();
- // });
- },
- getUrlParameter = function(name){
- return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
- },
- authorizeUser = function(){
- /* var XHR = new XMLHttpRequest();
- var sendData = {
- grant_type : 'authorization_code',
- code : data.boxAuthorizationCode,
- client_id : data.clientId,
- client_secret : data.clientSecret
- }
- // We define what will happen if the data is successfully sent
- XHR.addEventListener('load', function(event) {
- alert('Yeah! Data sent and response loaded.');
- });
- // We define what will happen in case of error
- XHR.addEventListener('error', function(event) {
- alert('Oups! Something goes wrong.');
- });
- // We setup our request
- XHR.open('POST', 'https://www.box.com/api/oauth2/token');
- // We add the required HTTP header to handle a form data POST request
- XHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- XHR.setRequestHeader('Authorization', 'Bearer $token');
- // And finally, We send our data.
- XHR.send(sendData);
- */
- 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;
- },
- //If token request is successful, show "theForm" to allow users to enter Customer FolderName and Project Name
- tokenGranted = function(){
- alert("token granted!");
- data.appState = "success";
- dom.appWrapper.attr("data-state", data.appState);
- },
- //POST Request to Box.com to create folders
- createFolders = function(){
- var customerFolder, projectName;
- customerFolder = $("#customerFolder").val();
- projectName = $("#projectName").val();
- //Validation check to make sure user doesn't enter a blank field
- console.log(customerFolder);
- console.log(projectName);
- var sendData = {
- name : customerFolder,
- parent : {"id": "0"}
- }
- $.ajax({
- // More info at http://developers.box.com/docs
- // The URL to process the request
- type : 'POST',
- url : 'https://api.box.com/2.0/folders',
- //headers: {},
- contentType: 'application/json',
- crossDomain: true,
- data : JSON.stringify(sendData),
- beforeSend: function (xhr) {
- xhr.setRequestHeader("Authorization", "Bearer " + data.access_token)
- },
- dataType: "json",
- success: function(response) {
- console.log(response);
- }
- });
- // Return false to prevent submission:
- return false;
- },
- myPrivateMethod = function(){
- alert("you called me!");
- },
- init = function(){
- setupDom();
- setupAppState();
- addEventHandlers();
- document.getElementById('theForm').onsubmit = createFolders;
- };
- api.myPublicMethod = function(){
- alert("you called me!");
- };
- $(init);
- }(window.boxManager = window.boxManager || {}, jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement