65.9K
CodeProject is changing. Read more.
Home

How to fetch KLOUT score

Aug 17, 2012

CPOL

1 min read

viewsIcon

8995

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.

  1. Get the KLOUT API Key.
  2. Call the KLOUT service to fetch “KLOUT ID” and call KLOUT service to fetch “KLOUT SCORE”.

Get KLOUT API Key:

  1. Navigate to below URL & register: http://developer.klout.com/member/register
  2. Klout would send an activation link to mail mentioned during registration.
  3. Upon activation it would provide APIKEY.

Call KLOUT service to fetch “KLOUT ID” and call KLOUT service to fetch “KLOUT SCORE”.

  1. Create a new ASP.NET website.
  2. Design a page as below to capture KLOUT API KEY and network screen name. In this case I am working with Twitter screen.
  3. 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:

    • txtApiKey: takes KLOUT API key
    • txtScreenName: takes twitter screen name
    • lblKloutId: will hold KloutID when button (btnKloutId) is clicked
    • lblKloutScore: will hold KloutScore when button (btnKloutScore) is clicked
  4. Now handle events to for the buttons defined. Please refer to this URL for more details: http://klout.com/s/developers/v2.
  5. 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;
    }
  6. 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:
  7. public class KloutResponse
    {
        public string id { get; set; }
        public string network { get; set; }
    }
    
    public class KloutScore
    {
        public string score { get; set; }
    }
  8. Now execute the application & you should be able to see the output.

Happy coding… Hope this helps!