Using MySpace REST API with OAuth and C#

In this example we will make a REST call to MySpace, get back 100 of our friends and display them on a web page. Because we’re accessing personal private information, you need to pass some authorization parameters with your REST call, and MySpace has adapted the OAuth authorization standard. Before I start explaining more about the code, make sure you already have a MySpace account, then follow the next few steps:

  1. Go to: http://developer.myspace.com/ and
  2. Click on Build on the top nav.
  3. Select Create on-site app button, and fill out your app info.
  4. When you get to the section for upload app xml, click on skip this step.
  5. Make a note of your OAuth Consumer Key and OAuth Consumer Secret because you will need it later.
  6. Under the Canvas Surface tab, select External IFrame option.
  7. Then you have to add the app, otherwise you may get 401 access denied error attempting to make a request.

Okay, hit save and you should be good for now. Let’s open up Visual Studio 2008 and begin!

  1. Create a Website called MySpaceApp or whatever you want.
  2. Copy this file OAuthBase.cs into your project
  3. Add using OAuth namespace at the top of Default.cs
  4. Declare variables for your consumer key, consumer secret and request server:
    1
    2
    3
    4
    5
    
    <code>
    protected string consumerKey = "http://www.myspace.com/123456789";
            protected string consumerSecret = "12abcd345678efghi90jk";
            protected string requestServer = "http://api.myspace.com";
    </code>
  5. Create a public MakeRequest() method returning a DataSet.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    
    <code>
    public DataSet MakeRequest(string restCall)
            {
                // Form the full REST request url
                Uri url = new Uri(requestServer + restCall);
                DataSet ds = new DataSet();
     
                // Instantiate OAuthBase and declare variables
                OAuthBase oAuth = new OAuthBase();
                string nonce = oAuth.GenerateNonce();
                string timeStamp = oAuth.GenerateTimeStamp();
                string normUrl = string.Empty;
                string normParams = string.Empty;
                string strRequest = string.Empty;
     
                // Create an OAuth signature
                string signature = oAuth.GenerateSignature(url,
                    consumerKey, consumerSecret, string.Empty, string.Empty,
                    "GET", timeStamp, nonce, OAuth.OAuthBase.SignatureTypes.HMACSHA1,
                    out normUrl, out normParams);
     
                // Construct the OAuth authenticated REST url
                strRequest = normUrl + "?" + normParams + "&" + UrlEncode("oauth_signature") + "=" + UrlEncode(signature);
     
                return MakeWebRequest(strRequest);
            }
    </code>
  6. Next create a MakeWebRequest() method returning a DataSet
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    <code>
    protected DataSet MakeWebRequest(string restCall)
            {
                // Make web request
                HttpWebRequest request = WebRequest.Create(restCall) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    // In this example we are getting back XML
                    XmlTextReader xReader = new XmlTextReader(response.GetResponseStream());
     
                    DataSet ds = new DataSet();
                    ds.ReadXml(xReader);
                    xReader.Close();
     
                    return ds;
                }
            }
    </code>
  7. Finally create a GetFriendList() method that will make the REST request and bind it to a Repeater displaying all your friends.

    1
    2
    3
    4
    5
    6
    7
    
    <code>
        protected void GetFriendList()
        {
            rptFriends.DataSource = fb.MakeRequest("/v1/users/YOUR_USER_ID/friends.xml?page_size=100");
            rptFriends.DataBind();
        }
    </code>
  8. I also built this REST Test Tool that uses OAuthBase.cs that will help you create sample REST calls so you can validate the OAuth parameters.
[del.icio.us] [Digg] [Facebook] [LinkedIn] [MySpace] [StumbleUpon] [Twitter] [Windows Live] [Yahoo!] [Email]

Popularity: 100% [?]

Read More