Skip to content

Instantly share code, notes, and snippets.

@allenmichael
Created September 20, 2017 13:43
Show Gist options
  • Save allenmichael/1ec1c19b2a538abaa415197dca255b6f to your computer and use it in GitHub Desktop.
Save allenmichael/1ec1c19b2a538abaa415197dca255b6f to your computer and use it in GitHub Desktop.
'use strict';
const box = require('box-node-sdk');
const fs = require('fs');
let configFile = fs.readFileSync('config.json');
configFile = JSON.parse(configFile);
let session = box.getPreconfiguredInstance(configFile);
let serviceAccountClient = session.getAppAuthClient('enterprise');
serviceAccountClient.enterprise.addAppUser("externalID", { "external_app_user_id": "auth0ID" })
.then((user) => {
console.log(user);
})
.catch((err) => {
handleConflictError(err);
});
const handleConflictError = (err) => {
let userID;
if (err && err.response && err.response.body && err.response.body.context_info &&
err.response.body.context_info && err.response.body.context_info.conflicts) {
if (err.response.body.context_info.conflicts.length > 0) {
userID = err.response.body.context_info.conflicts[0].id;
console.log("User already exists");
console.log(userID);
}
}
}
'use strict';
const box = require('box-node-sdk');
const fs = require('fs');
let configFile = fs.readFileSync('config.json');
configFile = JSON.parse(configFile);
let session = box.getPreconfiguredInstance(configFile);
let serviceAccountClient = session.getAppAuthClient('enterprise');
serviceAccountClient.enterprise.getUsers({ "external_app_user_id": "auth0ID" })
.then((userList) => {
let user;
if (userList.entries.length > 0) {
user = userList.entries[0];
return session.getAppUserTokens(user.id);
} else {
throw new Error("Couldn't locate user with that external ID");
}
})
.then((token) => {
console.log(token);
})
.catch((err) => {
console.log(err);
});
const box = require('box-node-sdk');
const fs = require('fs');
const boxRevokeURL = "https://api.box.com/oauth2/revoke";
const token = "S0VepoL6awRGHJ2l7XI51ezyWIAavRbs";
let configFile = fs.readFileSync('config.json');
configFile = JSON.parse(configFile);
let client = new box({
clientID: '',
clientSecret: ''
}).getBasicClient();
const revokeToken = (token) => {
let params = {
form: {
client_id: configFile.boxAppSettings.clientID,
client_secret: configFile.boxAppSettings.clientSecret,
token,
}
};
client.post(boxRevokeURL, params)
.then((response) => {
console.log(response.statusCode)
if (response.statusCode >= 200 && response.statusCode <= 299) {
console.log("Success!");
} else {
console.log(response.body);
console.log("Failure");
}
});
}
revokeToken(token);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment