开发者

Newbie Needs help with basic MVC Oauth Twitter set up

I'm a hobby developer and I am fiddlng around with asp.net MVC and was trying to just get the basic Oauth to work via Twitter.

All I did was put a controller named Twitter with this code: (it's from an online example for webforms but i slightly modified it and put it into 2 action methods, index and callback.. is this right way to do it ?)

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using OAuthLibrary;

namespace mvcTwit.Controllers
    {
        public class TwitterController : Controller
        {
            private const string AccessUrl =     "http://twitter.com/oauth/access_token";

    private const string AuthorizeUrl =  "http://twitter.com/oauth/authorize?oauth_token={0}";

    private const string RequestUrl =    "http://twitter.com/oauth/request_token";

    //
    // GET: /Twitter/
    public ActionResult Index()
    {

        // add these to web.config
        var consumerKey =    ConfigurationManager.AppSettings["consumerKey"];
        var consumerSecret = ConfigurationManager.AppSettings["consumerSecret"];

        // look for an access token in the callback
        var requestToken = Request.QueryString["oauth_token"];

        if (requestToken == null)
        {
            requestToken = OAuth.GetRequestToken(RequestUrl,
                                                 consumerKey,
                                                 consumerSecret);
            var collection = HttpUtility.ParseQueryString(requestToken);
            var authorizeUrl = String.Format(AuthorizeUrl,
                                             collection[0]);

            Response.Redirect(authorizeUrl);
        }
        return View();
    }

    public ActionResult Callback(string oauth_token)
    {
        var consumerKey = ConfigurationManager.AppSettings["consumerKey"];
        var consumerSecret = ConfigurationManager.AppSettings["consumerSecret"];
        //var requestToken = Request.QueryString["oauth_token"];
        var requestToken = oauth_token;

        // oauth is complete and callback is returning 
        // the possibly authorized request token
        var collection = HttpUtility.ParseQueryString(requestToken);

        // obtain access token
        var accessToken = OAuth.GetAccessToken(AccessUrl,
                                               consumerKey,
                                               consumerSecret,
                                               collection[0],
                                               collection[1]);
        collection = HttpUtility.ParseQueryString(accessToken);

        // make a Twitter request with the access token and secret
        var url = "http://twitter.com/account/verify_credentials.xml";
        var verify = OAuth.GetProtectedResource(url,
       开发者_StackOverflow中文版                                         "GET",
                                                consumerKey,
                                                consumerSecret,
                                                collection[0],
                                                collection[1]);

        ViewData["oauth_token"] = verify;
        return View();
    }

}

}

When i go to mysite.com/Twitter, it does its thing and takes me to twitter.com/oauth/authorize?oauth_token=(long string here)

Then after i fill in my u/n and p/w, it takes me back to my site: mysite.com/Twitter/callback?oauth_token=(long string)

but the error on the page is:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

My question is, is the signature of the Callback action correct because it expects a string back from twitter. And obviously I need to add a route in my global.asax file. What would that route look like?.. I have tried everything and i can't get it to work. Is the root of my issue or am i making a programming mistake..lol

Don't be hard on me, i'm no expert, but just learning as i go.

And, I'm testing this on a website, not localhost.

Thank You.

p.s. I have spent way to long on this and am looking for help as a last resort, so thanks for your kindness.


Looks like var collection = HttpUtility.ParseQueryString(requestToken); is not returning you two results as you expect;

  var accessToken = OAuth.GetAccessToken(
        AccessUrl, consumerKey, consumerSecret,
        collection[0], collection[1]);   <<-- this line is your problem?

I think you need to split the collection[0] into the two Oauth token parts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜