Last active
November 21, 2019 21:16
-
-
Save sanatem/6925ebf93819e96ce39887e8899e5c04 to your computer and use it in GitHub Desktop.
Show Rooms from the Webex API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function oauthFlowCompletedShowRooms(access_token, res) { | |
// Retreive rooms from user: GET https://api.ciscospark.com/v1/rooms | |
const options = { | |
method: 'GET', | |
url: 'https://api.ciscospark.com/v1/rooms', | |
headers: | |
{ | |
"authorization": "Bearer " + access_token | |
} | |
}; | |
request(options, function (error, response, body) { | |
if (error) { | |
debug("could not reach Webex API to retreive Room's details, error: " + error); | |
res.send("<h1>OAuth Integration could not complete</h1><p>Sorry, could not retreive your Webex Teams room details. Try again...</p>"); | |
return; | |
} | |
// Check the call is successful | |
if (response.statusCode != 200) { | |
debug("could not retreive your room details, /rooms returned: " + response.statusCode); | |
res.send("<h1>OAuth Integration could not complete</h1><p>Sorry, could not retreive your Webex Teams room details. Try again...</p>"); | |
return; | |
} | |
// Check JSON payload is compliant with specs https://api.ciscospark.com/v1/people/me | |
// { | |
// "items": [ | |
// { | |
// "id": "Y2lzY29zcGFyazovL3VzL1JPT00vOTVhZThjYmEtMjdlYy0zYzcyLTljODQtMmY0ZjJiMjAyYWU4", | |
// "title": "Help Bot", | |
// "type": "direct", | |
// "isLocked": false, | |
// "lastActivity": "2019-10-28T19:01:00.976Z", | |
// "creatorId": "Y2lzY29zcGFyazovL3VzL1BFT1BMRS84N2IyODQzMi0zMjAyLTQzOWEtYTM1ZC1mMjc1ZDI0NDA2OGE", | |
// "created": "2019-10-28T19:00:52.838Z", | |
// "ownerId": "e17d4b4d-d1d4-4ebd-8b20-7735538ecf7b" | |
// }, | |
const json = JSON.parse(body); | |
if ((!json) || (!json.items)) { | |
debug("could not parse Room details: bad json payload or could not find a displayName."); | |
res.send("<h1>OAuth Integration could not complete</h1><p>Sorry, could not retreive your Webx Teams room details. Try again...</p>"); | |
return; | |
} | |
// Uncomment to send feedback via static HTML code | |
//res.send("<h1>OAuth Integration example for Webex (static HTML)</h1><p>So happy to meet, " + json.displayName + " !</p>"); | |
// Current code leverages an EJS template: | |
const str = read(join(__dirname, '/togofurther/list-rooms.ejs'), 'utf8'); | |
const compiled = ejs.compile(str)({ "rooms": json.items }); | |
res.send(compiled); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment