boxSDK
The client can be used to make Oauth easier, do get,post,put and delete calls and handle file uploads.
Authentication
First make a client builder and pass the app ID and secret.
The client Builder should be outside of any requests, that way it's accessible from all requests; it's there so you don't have to give the client your appID and secret each time you need it.
Setup Client Factory:
var boxSDK = var appSettings = appID: 'YOUR APP IDD' secret: 'YOUR APP SECRET' callBackURL : 'YOUR CALLBACK URL' // only if you need this var clientBuilder = boxSDKclientBuilder
In your first request get the authurl and redirect the user.
Authentication URL:
var client = clientBuilder var path = clientauthURL res
Then pass the auth code to the client and handle with a callback function (i.e. store tokens in cookie like in example).
Retrieve Access Tokens:
var client = clientBuilder { rescookie'accessToken'responseaccess_tokenmaxAge: 60000 * 59 rescookie'refreshToken'responserefresh_tokenmaxAge: 60000 * 59 res } var path = client
You can also revoke tokens, it will respond with 200 if logout was successful or 400 if it was not.
Revoke Access Tokens
var client = clientBuilder { ifstatuscode == 200 console else console } client
Lastly you can refresh your access token
Refresh Tokens:
var client = clientBuilder { rescookie'accessToken'responseaccess_tokenmaxAge: 60000 * 59 rescookie'refreshToken'responserefresh_tokenmaxAge: 60000 * 59 res } var path = client
Extra information regarding client builder
clientBuilder.create() can take a string or an object, if you supply a string it will automatically assign that as the 'ACCESS_TOKEN'. It can also take a user's ID for when you want to do something 'as-user' but it requires the person logged in to be an Admin but it is optional. An object requires it to be formated as :
var tokens = access_token : 'ACCESS_TOKEN' refresh_token : 'REFRESH_TOKEN' user : 'user_id'
URL Builder:
There is also the url builder you can use to make url building easier and more semantically readable.
var urlB = boxSDKurlBuilder var path = urlBhost'auth'object'token'
There are three 'hosts' you can use:
- 'auth' for oauth authentication (unneeded as the client has built in methods for this)
- 'api' for api access
- 'upload' for uploading files to box
Making Requests
You can use the client to make get or post requests like so.
Get folders:
var client = clientBuilder var path = urlBhost'api'object'folders'action'items' { console console } client
Create folders:
var client = clientBuilder var path = urlBhost'api'object'folders' var data = "name":"New Folder" "parent": "id": "11446498" { console console } client
File uploading:
Currently the client supports uploading files from the server to box, the best way to do this is to use the method Layer function 'uploadFile'.
It will open the file and handle filename and formating so box can understand it.
The file object requires specific formating:
- filename = The whole filepath (the file name itself is taken automatically)
- parent_id = The id of the folder you wish to upload to
The function requires a client, a file object and a callback, you can use it like so:
File Upload with methodlayer function:
var fileObject = filename: __dirname + '/b.docx' parent_id: idboxSDKfiles
You can also supply the file data directly to the object, it MUST however be in binary encoding otherwise the file won't upload correctly.
Just place the data in the fileObject under fileObject.data
and the client will pick it up when the object is passed to it.
If you aren't sure the file data isn't in binary you can convert it with:
var fileObjectdata = fileData
uploading with streams
You can also pipe data to the method as well, as long as the data piped to it is either raw file data (from a file stream) or raw multiform data (non parsed by a body parser) then file upload should be able to handle it all.
File Upload with methodlayer function via streams
var file = filename: __dirname + '/a.jpg' parent_id: idf = fsf
In case you do want to use the client directly you can still use it, but it can't handle streams, if you want to pass it file data you need to pass it file f under fileObject.data
Client upload:
var fileObject = filename: __dirname + '/b.docx' parent_id: id data : data var path = urlBhost'upload'object'files'url client
Other functions
The purpose of the method layer is to make using the client easier, you don't need to use it to make use of the client but it makes using the box API easier and they contain helper functions so you don't have to do so much.
The functions are relatively semantic so you can access them like:
boxsdkfolders boxsdkusers boxsdkcommentsupdateclientdata'COMMENT_ID'callBackFunction
The call-backs are always implemented as :
{ }
Functions implemented are:
- Folder (all functions)
- Files (all functions)
- Comments (all functions)
- Collaborations (all functions)
- Events (no functions)
- Search (all functions)
- Users (all functions)
- Groups (all functions)
- Tasks (all functions)