Yammer is an enterprise social network service that was launched in September 2008.Unlike Twitter, which is used for broadcasting messages to the public, Yammer is used for private communication within organizations or between organizational members and pre-designated groups, making it an example of enterprise social software. Yammer originally launched as an enterprise micro blogging service and has evolved to become a fully-fledged enterprise social network.
Yammer Offer its API to create personalized yammer applications for mobile phones and desktop, to in this tutorial we will make and application, authorize it and access data from user profile. To access data from yammer api one passed from following steps
- Register your Application on Yammer and get Application Secret and Application Key.
- Then get Token and Token secret from above data.
- Then Authorize your application using the token and get the Auth Code.
- Then Auth Code, token and token get the access token and access token secret.
- Finally get Data from the user profile.
And now get all the obvious stuff out of the way and
let the code talk :) i made a small application demonstrating said steps.
 |
| Click on Image for full size |
and the code...
<?php
error_reporting(0);
require_once 'common.inc.php';
require_once 'Yammer.php';
$appKey="YOUR APP KEY HERE";
$appSec="YOUR APP SECRET HERE";
$token="";
$tokenSec="";
$authcode="";
$accessToken="";
$accessTokenSec="";
$b=false;
if(isset ($_POST["isSubmit"])){
$appKey=$_POST["appkey"]==""?"YOUR APP KEY HERE":$_POST["appkey"];
$appSec=$_POST["appsec"]==""?"YOUR APP SECRET HERE":$_POST["appsec"];
$token=$_POST["token"];
$tokenSec=$_POST["tokensec"];
$authcode=$_POST["authcode"];
$accessToken=$_POST["accessToken"];
$accessTokenSec=$_POST["accessTokenSec"];
$test_consumer = new OAuthConsumer($appKey, $appSec, NULL);
if(isset ($_POST["reqtoken"])){
$endpoint="https://www.yammer.com/oauth/request_token";
$req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", $endpoint, null);
$req_req->sign_request($plaintext_method , $test_consumer, NULL);
$data=getContents($req_req->to_url());
$data= explode("&", $data);
$token= str_replace("oauth_token=", "", $data[0]);
$tokenSec= str_replace("oauth_token_secret=", "", $data[1]);
$b=true;
}
if(isset ($_POST["acctoken"])){
$endpoint="https://www.yammer.com/oauth/access_token";
$param=Array('oauth_verifier'=>"$authcode");
$test_token = new OAuthConsumer($token, $tokenSec);
$acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, "GET", $endpoint, $param);
$acc_req->sign_request($plaintext_method, $test_consumer, $test_token);
$data=getContents($acc_req->to_url());
$data= explode("&", $data);
$accessToken= str_replace("oauth_token=", "", $data[0]);
$accessTokenSec= str_replace("oauth_token_secret=", "", $data[1]);
}
if(isset ($_POST["getData"])){
$obj=new Arc90_Service_Yammer($appKey, $appSec , $accessToken, $accessTokenSec, 10);
$json=json_decode($obj->getMessagesAll());
$k =count($json->messages);
echo "Messages Retrived via API
"; for( $i=0;$i<$k;$i++){ echo "
- ".$json->messages[$i]->body->plain."
"; } echo "
";
}
}
function getContents($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
return $data;
}
?>
and the html code ...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
input{
font-size:24px;
color:#666;
}
body {
font-size:20px;
font-family: Verdana,Arial,Helvetica,sans-serif;
}
.style7 {
color: #990033;
font-weight: bold;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Yammer OAuth & API Test - Originative Systems</title>
</head>
<body>
<form action="" method="post">
<table width="100%" border="1" style="border-collapse:collapse; border:solid 2px outset" cellpadding="8" cellspacing="5">
<tr>
<td colspan="5"><span class="style7">Yammer OAuth & API Test</span> </td>
</tr>
<tr>
<td width="25%"><div align="right">Application Key : </div></td>
<td width="75%" colspan="4"><label>
<input name="appkey" type="text" value="<?php echo $appKey; ?>" id="appkey" size="40" />
</label></td>
</tr>
<tr>
<td><div align="right">Application Secrect : </div></td>
<td colspan="4"><label>
<input name="appsec" type="text" value="<?php echo $appSec; ?>" id="appsec" size="40" />
</label></td>
</tr>
<tr>
<td><div align="right">Token : </div></td>
<td colspan="4"><label>
<input name="token" type="text" value="<?php echo $token; ?>" id="token" size="40" />
</label></td>
</tr>
<tr>
<td><div align="right">Token Secrect : </div></td>
<td colspan="4"><label>
<input name="tokensec" type="text" value="<?php echo $tokenSec; ?>" id="tokensec" size="40" />
</label>
<?php if($b){
echo "<INPUT type='button' value='Authorize Request' onClick=\"window.open('https://www.yammer.com/oauth/authorize?oauth_token=$token','Authorize Yammer Request','width=500,height=500')\"> ";
}
?>
</td>
</tr>
<tr>
<td><div align="right">Auth Code : </div></td>
<td colspan="3"><label>
<input name="authcode" type="text" value="<?php echo $authcode; ?>" id="token" size="4" />
</label></td>
</tr>
<tr>
<td><div align="right">Access Token : </div></td>
<td colspan="4"><label>
<input name="accessToken" type="text" value="<?php echo $accessToken; ?>" id="token" size="40" />
</label></td>
</tr>
<tr>
<td><div align="right">Access Token Secret : </div></td>
<td colspan="4"><label>
<input name="accessTokenSec" type="text" value="<?php echo $accessTokenSec; ?>" id="token" size="40" />
</label></td>
</tr>
<tr>
<td> </td>
<td align="center"><label>
<input name="reqtoken" type="submit" id="reqtoken" value="Request Token" />
</label>
</td>
<td align="center"><label>
<input name="acctoken" type="submit" id="acctoken" value="Access Token" />
</label></td>
<td align="center"><label>
<input name="getData" type="submit" id="acctoken" value="Get Data" />
</label></td>
</tr>
</table>
<input type="hidden" name="isSubmit" value=""/>
</form>
</body>
</html>
just run it and you will understand it, there nothing left to explain after source code, download netbeans project with all the files used and enjoy :)
 |
| Download Yammer API Example |