Last active
December 12, 2015 06:18
-
-
Save nmccarthy/4727823 to your computer and use it in GitHub Desktop.
An answer to a question posed by Jan van Casteren in the Yammer Developers Network (https://www.yammer.com/yammerdevelopersnetwork). The yam.request method of the Yammer Javascript API will query a user's home network by default. To query any other network, you have to overwrite the HTTP header to use the appropriately token for the user (i.e. t…
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
<script type="text/javascript" data-app-id="YOUR-APP-ID" src="https://assets.yammer.com/platform/yam.js"></script> | |
<script> | |
yam.getLoginStatus(function(response) { | |
if (response.authResponse) | |
{ | |
//GET THE TOKENS FOR ALL MY NETWORKS | |
yam.request({ | |
url: "https://www.yammer.com/api/v1/oauth/tokens.json", | |
method: "GET", | |
success: function(msg) { | |
var access_token = ""; | |
for (var i=0; i < msg.length; i++) { | |
//GRAB THE TOKEN FOR THE DESIRED NETWORK | |
if (msg[i].network_permalink == "TARGET_NETWORK_PERMALINK") | |
{ | |
console.log(msg[i].network_permalink) | |
access_token = msg[i].token; | |
break; | |
} | |
}; | |
//request messages for the target network | |
yam.request({ | |
url: "https://www.yammer.com/api/v1/messages.json", | |
method: "GET", | |
beforeSend: function (req) { //send the access_token in the HTTP header | |
req.headers.Authorization = "Bearer " + access_token; | |
}, | |
success: function(msg) { | |
console.log(msg); | |
}, | |
error: function(msg) { | |
console.log(msg); | |
} | |
}); | |
}, | |
error: function(msg) { | |
console.log(msg); | |
} | |
}); | |
} | |
else | |
{ | |
yam.login(function(response) { | |
if (response.authResponse) | |
{ | |
console.log("you're logged in now, please reload the page"); | |
} | |
}); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment