|
|
|
|
Get Friends InformationFrom $1Table of contentsSee it in action
Check out the code
<html>
<head>
<title>Gigya Social Demo - getFriends</title>
<style>
body {font-family:Arial;font-size: 12px; background-color:#fff; line-height:20px;margin:1px;}
h5 { font-size: 12px; color: #6e6e6e; padding: 0px; margin: 0px; }
h6 { font-size: 14px; color: #6e6e6e; padding: 0px; margin: 0px; font-weight:bold; }
</style>
<SCRIPT type="text/javascript" lang="javascript"
src="http://cdn.gigya.com/JS/socialize.js?apikey=2_Y82PzwJ_chSFImHXaIDJClnLyJzmk-VFOavSsaNTzl6m901s_NNxRAS0xJ3bd3_N">
</SCRIPT>
<script>
var conf =
{
APIKey: '2_Y82PzwJ_chSFImHXaIDJClnLyJzmk-VFOavSsaNTzl6m901s_NNxRAS0xJ3bd3_N'
};
function onLoad() {
// get user info
gigya.services.socialize.getUserInfo(conf, { callback: renderUI });
// register for connect status changes
gigya.services.socialize.addEventHandlers(conf,
{ onConnect: renderUI, onDisconnect: renderUI });
}
</script>
<script type="text/javascript">
function renderUI(res) {
// enable/disable "Get Friends" button
var connected = (res.user != null && res.user.isConnected);
document.getElementById('btnGetFriends').disabled = !connected;
// clear friend list if not connected
if (!connected)
document.getElementById('friends').innerHTML = "";
}
// Get the user's friends
function getFriends() {
gigya.services.socialize.getFriendsInfo(conf, { callback: getFriends_callback });
document.getElementById('btnGetFriends').disabled = true;
}
// Use the reponse of getFriends and render HTML to display the first five friends.
function getFriends_callback(response) {
document.getElementById('btnGetFriends').disabled = false;
document.getElementById('friends').innerHTML = "";
if (response.status == 'OK') {
var array = response.friends.asArray();
var html = "You have " + array.length + " friends, here are a few of them:<BR/>";
html += "<table cellpadding=20><tr>";
for (var i = 0; i < Math.min(5, array.length); i++) {
html += "<td align=center valign='bottom'>";
if (array[i].thumbnailURL)
html += "<img width='50' height='50' src='"
+ array[i].thumbnailURL + "' ><br>";
html += array[i].nickname + "</td>";
}
html += "</tr></table>";
document.getElementById('friends').innerHTML = html;
}
}
</script>
</head>
<body onload="onLoad()">
<h5>Step 1: Connect</h5>
<div id="divConnect"></div>
<script type="text/javascript">
// show connect UI in "divConnect"
gigya.services.socialize.showConnectUI(conf, {
width: 190,
showTermsLink: 'false', // remove 'terms' link
hideGigyaLink: 'true', // remove 'Gigya' link
useFacebookConnect: 'true', // use Facebook connect when connecting to Facebook
containerID: "divConnect" // The component will embed itself inside the divConnect Div
});
</script>
<br />
<h5>Step 2: Get Friends</h5>
<div style="margin-top:5px;">
Click the button below to retrieve your social network friends
</div>
<input id="btnGetFriends" type="button" value="Get Friends"
onclick="getFriends()" disabled=true/>
<div id="friends"></div>
</body>
</html>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="693"
height="579" applicationComplete="init()" fontFamily="Arial"
fontSize="12" backgroundGradientAlphas="[1.0, 1.0]"
backgroundGradientColors="[#FFFFFF, #FFFFFF]">
<mx:Canvas x="29" y="57" id="connectCanvas" width="325" height="103">
</mx:Canvas>
<mx:Label x="29" y="31" text="Step 1: Connect" width="113" color="#6E6E6E"
fontWeight="bold" fontSize="12" fontFamily="Arial"/>
<mx:Label x="29" y="183" text="Step 2: Get Friends" color="#6E6E6E"
fontWeight="bold" fontFamily="Arial" fontSize="12"/>
<mx:Label x="29" y="209" width="352" fontFamily="Arial">
<mx:text>Click the button below to retrieve your social network friends</mx:text>
</mx:Label>
<mx:Button id="getFriendsBtn" x="29" y="235" label="Get Friends" width="94"
enabled="false" click="getFriends()"/>
<mx:Canvas id="friendsCanvas" x="29" y="268" width="610" height="270">
</mx:Canvas>
<mx:Script>
<![CDATA[
import mx.controls.Image;
import mx.controls.Label;
// Define a configuration object. Insert your Gigya APIKey below:
private var conf:Object = {
APIKey: '2_Y82PzwJ_chSFImHXaIDJClnLyJzmk-VFOavSsaNTzl6m901s_NNxRAS0xJ3bd3_N'
}
// This method is executed on applicationComplete
private function init():void {
// Initialize the "mcRoot" attribute of the conf object - should refer
// to the root of the flash container.
this.conf.mcRoot=this.root;
// Load socialize service
// Create the load-parameters object
var loadParams:Object = {
services:'socialize',
callback:onGigyaInit
}
// Load the service
gigya.load(conf,loadParams);
}
// Wait for the load to complete and handle failures/success
private function onGigyaInit(response:*):void {
if (response.hadError) {
// handle failure
trace('An error has occurred while attempting to load Socialize service');
} else {
// get user info
gigya.services.socialize.getUserInfo(conf,{callback:renderUI});
// register for connect status changes
gigya.services.socialize.addEventHandlers(conf,
{ onConnect:renderUI, onDisconnect:renderUI } );
showConnectUI();
}
}
// show connect UI in connectCanvas
private function showConnectUI(): void {
//Define parameters object:
var params:Object=
{
width: 190,
useFacebookConnect: true,
showTermsLink: 'false',
hideGigyaLink: 'true',
container: connectCanvas // The component will be embedded inside
// the connectCanvas
}
// Calling the Socialize API method - connect:
gigya.services.socialize.showConnectUI(conf, params);
}
private function renderUI(res:Object): void {
// enable/disable "Get Friends" button
var connected:Boolean = (res.user!=null && res.user.isConnected);
getFriendsBtn.enabled = connected;
friendsCanvas.visible = connected;
}
// Get the user's friends.
// This method is associated with the "getFriendsBtn" click
private function getFriends():void
{
gigya.services.socialize.getFriendsInfo(conf, {callback:getFriends_callback});
getFriendsBtn.enabled = false;
}
// Use the reponse of getFriends to display the first five friends.
private function getFriends_callback(response:Object):void
{
getFriendsBtn.enabled = true;
friendsCanvas.visible = true;
if (response.status == 'OK')
{
var array:Array = response.friends.asArray();
var caption:Label = new Label();
caption.text = "You have " + array.length
+ " friends, here are a few of them:";
friendsCanvas.addChild(caption);
for (var i:int = 0; i < Math.min(5,array.length); i++)
{
if(array[i].thumbnailURL){
var friendImage:Image = new Image();
friendImage.source = array[i].thumbnailURL;
friendImage.width = 50;
friendImage.height = 50;
friendImage.x = 120*i
friendImage.y = 50
friendsCanvas.addChild(friendImage);
var friendName:Label = new Label();
friendName.text = array[i].nickname;
friendName.x = 120*i;
friendName.y = 110;
friendsCanvas.addChild(friendName);
}
}
}
}
]]>
</mx:Script>
</mx:Application>
Note: In order to make this code work in your environment, please:
Tags:
|
||||
|