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:
- Go to: http://developer.myspace.com/ and
- Click on Build on the top nav.
- Select Create on-site app button, and fill out your app info.
- When you get to the section for upload app xml, click on skip this step.
- Make a note of your OAuth Consumer Key and OAuth Consumer Secret because you will need it later.
- Under the Canvas Surface tab, select External IFrame option.
- 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!
- Create a Website called MySpaceApp or whatever you want.
- Copy this file OAuthBase.cs into your project
- Add using OAuth namespace at the top of Default.cs
- 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>
- 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>
- 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>
-
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>
- 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.
Popularity: 100% [?]
Read More
![[del.icio.us]](http://stevenng.net/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://stevenng.net/wp-content/plugins/bookmarkify/digg.png)
![[Facebook]](http://stevenng.net/wp-content/plugins/bookmarkify/facebook.png)
![[LinkedIn]](http://stevenng.net/wp-content/plugins/bookmarkify/linkedin.png)
![[MySpace]](http://stevenng.net/wp-content/plugins/bookmarkify/myspace.png)
![[StumbleUpon]](http://stevenng.net/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Twitter]](http://stevenng.net/wp-content/plugins/bookmarkify/twitter.png)
![[Windows Live]](http://stevenng.net/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://stevenng.net/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://stevenng.net/wp-content/plugins/bookmarkify/email.png)