Last active
August 29, 2015 14:03
-
-
Save Salvodif/1ddb076e482ccf36caf1 to your computer and use it in GitHub Desktop.
Yammer API Example
This file contains hidden or 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
To get the APP-ID-CODE you have to create a new Yammer APP likes you can see in this pic: https://onedrive.live.com/redir?resid=326B1FC9A1CE351F!15015&authkey=!AMmmiBkza5_idPM&v=3&ithint=photo%2c.png | |
Also is IMPORTANT allows javascript calls from the source origin. | |
To do this you have to specify the source origin on the app info: https://onedrive.live.com/redir?resid=326B1FC9A1CE351F!15016&authkey=!AJ62DteVIedu_q0&v=3&ithint=photo%2c.png |
This file contains hidden or 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
<script type="text/javascript" data-app-id="APP-ID-CODE" src="https://assets.yammer.com/assets/platform_js_sdk.js"></script> | |
<div id='page'> | |
<div> | |
<h2>JS SDK</h2> | |
<button id="yammer-js-login-button">JS Login</button> | |
<button id="yammer-user-button">Get Current User</button> | |
<button id="yammer-group-button">Get Current Groups</button> | |
</div> | |
<div class="logged-in" style="display: none"> | |
<p>User is now signed in to the app using Yammer</p> | |
<button id="disconnect" class="yj-btn yj-btn-alt">Log out from your Yammer account</button> | |
</div> | |
<div class="logged-in" style="display: none"> | |
<h2>Authentication Logs</h2> | |
<pre id="authResult"></pre> | |
</div> | |
<div style="clear: both"> </div> | |
<div> | |
<button id="get-messages">Get messages</button> | |
<div class="messages-in" style="display: none"> | |
<h2>Messages</h2> | |
<pre id="messages"></pre> | |
</div> | |
</div> | |
<div style="clear: both"> </div> | |
<div> | |
<input type="text" id="message" /> | |
<button id="send-message">Send message</button> | |
</div> | |
</div> |
This file contains hidden or 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
yam.connect.embedFeed ( { | |
container : '#embedded-feed', | |
feedType : 'open-graph', | |
feedId : '', | |
config : { | |
use_sso : false, | |
header : true, | |
footer : true, | |
showOpenGraphPreview : false | |
}, | |
objectProperties : { | |
url : '', | |
type : 'page' | |
} | |
} ); | |
yam.connect.actionButton ( { | |
container : "#embedded-follow", | |
action : "follow" | |
} ); | |
yam.connect.actionButton ( { | |
container : "#embedded-like", | |
action : "like" | |
} ); | |
yam.config ( { | |
debug : true | |
} ); | |
function logout ( ) { | |
yam.platform.getLoginStatus ( | |
function ( response ) { | |
if ( response.authResponse ) { | |
yam.platform.logout ( function ( response ) { | |
toggleLoginStatus ( false ); | |
location.reload ( ); | |
} ); | |
} | |
} | |
); | |
} | |
function toggleLoginStatus ( loggedIn ) { | |
if ( loggedIn ) { | |
$ ( '.not-logged-in' ).hide ( ); | |
$ ( '.logged-in' ).show ( 'slow' ); | |
} else { | |
$ ( '.not-logged-in' ).show ( 'slow' ); | |
$ ( '.logged-in' ).hide ( ); | |
} | |
} | |
function displayAuthResult ( authResult ) { | |
console.log ( "AuthResult", authResult ); //print user information to the console | |
$ ( '#yammer-login' ).innerHTML = 'Welcome to Yammer!'; | |
toggleLoginStatus ( true ); | |
$ ( '#authResult' ).html ( 'Auth Result:<br/>' ); | |
for ( var field in authResult ) { | |
$ ( '#authResult' ).append ( ' ' + field + ': ' + authResult [ field ] + '<br/>' ); | |
} | |
$ ( '#authOps' ).show ( 'slow' ); | |
} | |
function getCurrentUser ( ) { | |
yam.platform.request ( { | |
url : "users/current.json", //this is one of many REST endpoints that are available | |
method : "GET", | |
data : { }, | |
success : function ( user ) { //print message response information to the console | |
console.log ( "User request was successful." ); | |
console.dir ( user ); | |
toggleLoginStatus ( true ); | |
$ ( '#authResult' ).html ( 'User Result:<br/>' ); | |
for ( var field in user ) { | |
$ ( '#authResult' ).append ( ' ' + field + ': ' + escape ( user [ field ] ) + '<br/>' ); | |
} | |
}, | |
error : function ( user ) { | |
console.error ( "There was an error with the request." ); | |
} | |
} ); | |
} | |
$ ( document ).ready ( function ( ) { | |
$ ( '#disconnect' ).click ( function ( ) { | |
yam.platform.getLoginStatus ( | |
function ( response ) { | |
if ( response.authResponse ) { | |
yam.platform.logout ( function ( response ) { | |
console.log ( "User was logged out" ); | |
location.reload ( ); | |
} ); | |
} else { | |
toggleLoginStatus ( false ); | |
} | |
} ); | |
} ); | |
$ ( '#yammer-js-login-button' ).click ( function ( ) { | |
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, "" ) ); | |
} | |
} ); | |
} | |
} ); | |
} ); | |
$ ( '#yammer-user-button' ).click ( getCurrentUser ); | |
$ ( '#yammer-group-button' ).click ( function ( ) { | |
yam.platform.request ( { | |
url : "groups.json?mine=1", | |
method : "GET", | |
data : { }, | |
success : function ( group ) { | |
$mygroup = ""; | |
for ( $i = 0; $i < group.length; $i++ ) { | |
$mygroup += '<img src="' + group [ $i ].mugshot_url + '">' + " " + group [ $i ].full_name + "," + "<br>"; | |
} | |
$ ( "#current-groups" ).html ( $mygroup ); | |
}, | |
error : function ( group ) { | |
console.error ( "There was an error with the request." ); | |
} | |
} ); | |
} ); | |
$ ( '#send-message' ).click ( function ( ) { | |
yam.platform.request ( { | |
url : "messages.json", | |
method : "POST", | |
data : { "body" : "Message Throug app", "topic1" : "#topic1Test" }, | |
success : function ( msg ) { alert ( "Post was Successful!: " + msg ); }, | |
error : function ( msg ) { | |
console.debug ( msg ); | |
alert ( "Post was Unsuccessful..." + msg ); | |
} | |
} ); | |
} ); | |
$ ( '#get-messages' ).click ( function ( ) { | |
yam.platform.request ( { | |
url : "messages.json", | |
method : "GET", | |
success : function ( msg ) { | |
$ ( '.messages-in' ).hide ( 'slow' ); | |
$ ( '#messages' ).delay( 800 ).empty ( ); | |
for ( var i = 0; i < msg.messages.length; i++ ) { | |
var obj = msg.messages [ i ]; | |
var body = obj.body.plain; | |
var created_at = obj.created_at; | |
var sender_id = obj.sender_id; | |
$ ( '#messages' ).append ( ' ' + body + ': ' + | |
created_at + ' : ' + sender_id + '<br/>===========================================================================================================<br/><br/>' ); | |
} | |
$ ( '.messages-in' ).show ( 'slow' ); | |
}, | |
error : function ( msg ) { | |
alert ( "No Go" ); | |
} | |
} ); | |
} ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment