tags:

views:

508

answers:

2

Hi,

I've been given 6 bits of information to access some data from a website:

  1. Website Json Url (eg: http://somesite.com/items/list.json)
  2. OAuth Authorization Url (eg: http://somesite.com/oauth/authorization)
  3. OAuth Request Url (eg: http://somesite.com/oauth/request)
  4. OAuth Access Url (eg: http://somesite.com/oauth/access)
  5. Client Key (eg: 12345678)
  6. Client Secret (eg: abcdefghijklmnop)

Now, I've looked at DotNetOpenAuth and OAuth.NET libraries, and while I'm sure they are very capable of doing what I need, I just can't figure out how to use either in this way.

Could someone post some sample code of how to consume the Url (Point 1.) in either library (or any other way that may work just as well)?

Thanks!

A: 

So far I have:

using OAuth.Net.Consumer;
using OAuth.Net.Components;

private static void Main()
{
    string key = "12345678";
    string secret = "abcdefghijklmnop";

    string jsonUrl = "http://somesite.com/items/list.json";

    string oauthRequest = "http://somesite.com/oauth/request";
    string oauthAuth = "http://somesite.com/oauth/authorization";
    string oauthAccess = "http://somesite.com/oauth/access";

    var epRequest = new EndPoint(oauthRequest);
    var epAccess = new EndPoint(oauthAccess);
    var eConsumer = new OAuthConsumer(key, secret);
    var uriAuth = new Uri(oauthAuth);

    var svcd = OAuthService.Create(
        epRequest,
        uriAuth,
        epAccess,
        eConsumer);

        //NullReferenceException after the line above.
}

However as mentioned I get a NullReferenceException when calling the OAuthService.Create method and I'm not sure why... None of my parameters are null that are being passed into it...

Any ideas?

Redth
Still no success here
Redth
A: 

For OAuth 2.0:

I learned that it's easiest to just put up the authentication page in an HTML window then trap the returned access_token. You can then do that using in client-side web browser.

For example, in MonoTouch it would be:

//
// Present the authentication page to the user
//
var authUrl = "http://www.example.com/authenticate";
_borwser.LoadRequest (new NSUrlRequest (new NSUrl (authUrl)));

//
// The user logged in an we have gotten an access_token
//
void Success(string access_token) {

    _web.RemoveFromSuperview();

    var url = "http://www.example.com/data?access_token=" + access_token;

    // FETCH the URL as needed
}

//
// Watch for the login
//
class Del : UIWebViewDelegate
{
    public override void LoadingFinished (UIWebView webView)
    {
        try {
            var url = webView.Request.Url.AbsoluteString;
            var ci = url.LastIndexOf ("access_token=");
            if (ci > 0) {
                var code = url.Substring (ci + "access_token=".Length);
                _ui.Success (code);
            }
        } catch (Exception error) {
            Log.Error (error);
        }
    }
}
Frank Krueger
Thanks, unfortunately I'm working with oauth 1.0a which I neglected to mention in the question...
Redth