How to fetch KLOUT score





4.00/5 (1 vote)
The steps to follow to use the KLOUT Service to fetch a KLOUT score using .NET.
Introduction
In this article we will see the steps to follow to use the KLOUT Service to fetch a KLOUT score using .NET.
- Get the KLOUT API Key.
- Call the KLOUT service to fetch “KLOUT ID” and call KLOUT service to fetch “KLOUT SCORE”.
Get KLOUT API Key:
- Navigate to below URL & register: http://developer.klout.com/member/register
- Klout would send an activation link to mail mentioned during registration.
- Upon activation it would provide APIKEY.
Call KLOUT service to fetch “KLOUT ID” and call KLOUT service to fetch “KLOUT SCORE”.
- Create a new ASP.NET website.
- Design a page as below to capture KLOUT API KEY and network screen name. In this case I am working with Twitter screen.
txtApiKey
: takes KLOUT API keytxtScreenName
: takes twitter screen namelblKloutId
: will hold KloutID when button (btnKloutId
) is clickedlblKloutScore
: will hold KloutScore when button (btnKloutScore
) is clicked- Now handle events to for the buttons defined. Please refer to this URL for more details: http://klout.com/s/developers/v2.
- As response from KLOUT is JSON object we need to de-serialize JSON object to respective entity. So to work with we need to have two entities as defined below:
- Now execute the application & you should be able to see the output.
Klout API KEY
<asp:TextBox ID="txtApiKey" runat="server"></asp:TextBox>
<br />
<br />
Twitter Screen name:
<asp:TextBox ID="txtScreenName" runat="server"></asp:TextBox>
<br />
<br />
Klout ID:
<asp:Label ID="lblKloutId" runat="server"></asp:Label>
<br />
<br />
Klout Score:
<asp:Label ID="lblKloutScore" Height="64px"
Width="416px" ReadOnly="true" TextMode="MultiLine"
runat="server"></asp:Label>
<br />
<br />
<asp:Button ID="btnKloutId" runat="server"
Text="Step 1: Get KloutID" OnClick="btnKloutId_Click" />
<asp:Button ID="btnKloutScore" runat="server"
Text="Step 2: Get Klout Score" OnClick="btnKloutScore_Click" />
Note:
protected void btnKloutId_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtApiKey.Text.Trim())
&& !string.IsNullOrEmpty(txtScreenName.Text.Trim()))
{
// as we are dealing with twitter so as the twitter param & we
// are expecting JSON as object so as identity.json &
// as we working v2 so as v2 in params
string url = string.Format("<a href="http://api.klout.com/v2/identity" +
".json/twitter?screenName={0}&key={1">http://api.klout" +
".com/v2/identity.json/twitter?screenName={0}&key={1</a>}",
txtScreenName.Text,
txtApiKey.Text);
string kloutResponse = GetResponse(url);
KloutResponse klot = General_Klout.Deserialize<KloutResponse>(kloutResponse);
this.lblKloutId.Text = string.Format("{0}", string.IsNullOrEmpty(klot.id) ? "N/A" : klot.id);
}
}
protected void btnKloutScore_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(lblKloutId.Text.Trim())
&& !lblKloutId.Text.Equals("N/A"))
{
string url = string.Format("<a href="http://api.klout" +
".com/v2/user.json/%7B0%7D/score?key={1">http://api." +
"klout.com/v2/user.json/{0}/score?key={1</a>}",
lblKloutId.Text, txtApiKey.Text);
string klotScoreString = GetResponse(url);
KloutScore klot = General_Klout.Deserialize<KloutScore>(klotScoreString);
this.lblKloutScore.Text = string.Format("{0}",
string.IsNullOrEmpty(klot.score) ? "N/A" : klot.score);
}
}
public static T Deserialize<T>(string json)
{
T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
return obj;
}
private string GetResponse(string url)
{
string strResult = string.Empty;
// declare httpwebrequet wrt url defined above
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
// set method as post
webrequest.Method = "GET";
// set content type
webrequest.ContentType = "application/x-www-form-urlencoded";
// declare & read response from service
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
// set utf8 encoding
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
// read response stream from response object
StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
// read string from stream data
strResult = loResponseStream.ReadToEnd();
// close the stream object
loResponseStream.Close();
// close the response object
webresponse.Close();
return strResult;
}
public class KloutResponse
{
public string id { get; set; }
public string network { get; set; }
}
public class KloutScore
{
public string score { get; set; }
}
Happy coding… Hope this helps!