Skip to content

Instantly share code, notes, and snippets.

@brianly
Created January 28, 2017 03:56
Show Gist options
  • Save brianly/8b1bdf88cfb01d510a7eaeafb3486adf to your computer and use it in GitHub Desktop.
Save brianly/8b1bdf88cfb01d510a7eaeafb3486adf to your computer and use it in GitHub Desktop.
Examples of Yammer group operations.
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script>
<script type="text/javascript" data-app-id="INSERT-CLIENT-ID [yammer.com/client_applications]" src="https://assets.yammer.com/assets/platform_js_sdk.js"></script>
</head>
<body>
<script>
function displayAuthResult(authResult) {
console.log("AuthResult", authResult);
$('#auth-result').html('Auth Result:<br/>');
for (var field in authResult) {
$('#auth-result').append(' ' + field + ': ' + authResult[field] + '<br/>');
}
}
function login() {
console.log("Trigger LoginStatus");
yam.platform.getLoginStatus(
function (response) {
if (response.authResponse) {
console.log("Logged in");
displayAuthResult(response.access_token);
localStorage.setItem(1, JSON.stringify(response.access_token.token).replace(/"/g, ""));
} else {
console.log("Not logged in. Going to login now.");
yam.platform.login(function (response) { //prompt user to login and authorize your app, as necessary
if (response.authResponse) {
displayAuthResult(response.access_token);
localStorage.setItem(1, JSON.stringify(response.access_token.token).replace(/"/g, ""));
}
});
}
}
);
}
function getCurrentUser() {
yam.platform.request({
url: "users/current.json",
method: "GET",
data: {},
success: function (user) {
console.log("User request was successful.");
console.dir(user);
$('#user-result').html('User Result:<br/>');
for (var field in user) {
$('#user-result').append(' ' + field + ': ' +
escape(user[field]) + '<br/>');
}
},
error: function (user) {
console.error("There was an error with getCurrentUser().");
$("#user-result").html("<p class='failure'>There was an error with the request. Did you authenticate?</p>");
}
});
}
function getCurrentGroups() {
yam.platform.request({
url: "groups.json?mine=1",
method: "GET",
data: {},
success: function (group) {
$mygroup = "";
for ($i = 0; $i < group.length; $i++) {
var groupLink = "<a target='_blank' href='" + group[$i].web_url + "'>" + group[$i].full_name + "</a> (" + group[$i].privacy + ")";
$mygroup += "<div><img src='" + group[$i].mugshot_url + "'>" + groupLink + "</div>";
}
$("#current-groups").html($mygroup);
},
error: function (group) {
console.error("There was an error with getCurrentGroups().");
$("#current-groups").html("<p class='failure'>There was an error with the request. Did you authenticate?</p>");
}
});
}
function createGroup() {
var name = $('#group-name-text').val();
var private = true;
var listed = 1; // 0 (hide from directory), or 1 (list in directory)
yam.platform.request({
url: "groups.json?name=" + name + "&private=" + private + "&show_in_directory=" + listed + "&description=aDescription",
method: "POST",
data: {},
success: function (group) {
$("#group-creation-result").html("<p class='success'>Created the <a target='_blank' href='" + group.web_url + "'>" + group.full_name + "</a> group in the " + group.network_name + " network.</p>");
},
error: function (group) {
$("#group-creation-result").html("<p class='failure'>There was an error with the request. Did you authenticate?</p>");
}
});
}
$(document).ready(function () {
$('#yammer-js-login-button').click(login);
$('#yammer-user-button').click(getCurrentUser);
$('#yammer-group-button').click(getCurrentGroups);
$('#group-create-button').click(createGroup);
});
</script>
<div>
<h2>1. Authenticate</h2>
<button id="yammer-js-login-button">JS Login</button>
<pre id="auth-result"></pre>
</div>
<div>
<h2>2. Fetch information for current user.</h2>
<button id="yammer-user-button">Get Current User Info</button>
<pre id="user-result"></pre>
</div>
<h2>3. Create a group</h2>
<div>
<input id="group-name-text" type="text" value=""><br>
<button id="group-create-button">Create Group</button>
</div>
<div>
<div id="group-creation-result"></div>
</div>
<h2>4. Fetch group memberships for current user</h2>
<div>
<button id="yammer-group-button">Get Current Groups</button>
</div>
<div>
<div id="current-groups"></div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment