The Great Getting Started Debate

Apr 16, 2011 at 1:46 AM
Edited Apr 16, 2011 at 1:47 AM

 

Hi All.

  I just started trying to play with this awesome library but I am having a lot issues getting up and running. I am able to edit some of the old MVC code but I am not really getting anywhere. I am able to use Linqpad and get the queries. I am having difficulty programing against the api in ASP,net i guess. I am not sure why I cannot do it.  The MVC code is using an old version of mvc and uses aspx instead of cshtmls. The winforms I am unable to test locally so it becomes painful in trial and error.


I am trying to do something simple and build on. I am trying to make a simple textbox where users can post to twitter using my own account and have a hashtag tracked underneath it.  From there ill build up further but I gotta start some where. 

This is a great library. I need to learn to program against it.  How do I get the winforms to work locally? I have some programming experience so I am not a totally incompetent.

Any other userful tips people learned would be great.

 

Thank you for your time.   I am determined to get my dream app done

Coordinator
Apr 16, 2011 at 2:51 AM

Hi,

There are a couple different things to track here, MVC and Windows Forms, which require different approaches for LINQ to Twitter.  For example, the MVC app will use the MvcAuthorizer to authenticate with OAuth.  OAuth was originally written to faciliatate Web apps (i.e. Web Forms or MVC) but it can now support desktop apps (i.e. Windows Forms) too.  The difference is that on the Web, you have a fluid workflow of redirection between the 3rd party service, the authenticating service, and the user.  I explain how Web authorization works in Introduction to OAuth. It needs more work, but also refers to other documentation that has more depth.  Desktop authorization works a little differently in that the workflow won't work the same way.  Therefore, Twitter supports a flavor of OAuth they call PIN authorization.  Instead of Twitter redirecting back to your application, it provides a PIN that you give to the desktop app to complete the workflow.  The LinqToTwitterDemo (C#) and LinqToTwitterDemoVB (for VB language code) show how to use PIN authorization.  They're console applications, but once you understand the workflow, translation to Windows Forms usually makes more sense.  So, in addition to different technologies, the way to do OAuth is different, which might relieve some confusion.

A couple questions:

  1. Are you using WebMatrix, building a Razor Web App?
  2. I'm not following what you mean when you say you can't run your Windows Forms locally.  Can you elaborate?  What tool are you using to do it? 

BTW, I normally use WPF for desktop development these days, especially if I'm starting a new application.  Windows Forms is easier in many ways, but is an older technology.

Joe

Apr 16, 2011 at 12:43 PM

Hi Joe,

 

  Thanks for such a speedy response.  

I am using Visual Developer Express 10 .  I used my 90 day trial of Visual Studio last year.

1.  Linq to twitter is not supported in web matrix.  It wont find its package under its nuget.  I was able to open up a project however. I totally understand webmatrix and coding that would be awesome.

2. I am unable to debug locally the windows forms sample. I even tried changing my local host to 127.0.0.1 and I get bad request.  But If I try to debug the sample MVC it works fine.  That why I am able to put break points on the MVC and see how things are working.

On an unrelated note, I spent the entire day watching Friday watching the entire Plural Sight MVC Course. lol.  That's one long series of videos. While it was good the  Music Store Sample was way better.

Back to topic. I noticed I was passing hash urls  back and forth thats why its called Oauth tokens.  I was playing with apigee.com/console and linqpad also.

 

I thought MVC might be easier because you have the classes in the controllers broken up into get and post.  Then I can just add the views behind the controller code using a model class. (i think that just popped in my head )

 

I am going to try to add breakpoints to a C console app and watch the tokens.

Apr 19, 2011 at 1:36 AM

i played a bit some more today.  creating forms in mvc is terrible with no UI.  Anyways. I searched the forums high and low I  know I am close.  This is what i got so far. I figured I would use the sample til i got something working then do the db stuff etc.  I am trying to post to twitter by clicking the button which calls the create action

 

using LinqToTwitter;
using System.Configuration;


namespace LinqToTwitterMvcDemo.Controllers
{
    public class TweetController : Controller
    {
        //
        // GET: /Tweet/
        [HttpGet]

        IOAuthCredentials credentials = new SessionStateCredentials();

        private MvcAuthorizer auth;
        private TwitterContext twitterCtx;

        public ActionResult Index()
        {
            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
            }

            auth = new MvcAuthorizer
            {
                Credentials = credentials
            };

            auth.CompleteAuthorization(Request.Url);

            if (!auth.IsAuthorized)
            {
                return auth.BeginAuthorization();
            }

            return View();

        }
  
    }
          

            //
            // Post: /Tweet
      
   

           [HttpPost]
            twitterCtx = new TwitterContext (auth);
         

            public ActionResult Create(string updateStatus);     
            
        {
            twitterCtx.UpdateStatus(updatestatus.Text);
            updateStatus.Text = string.Empty;
            successLabel.Visible = true;       
          
            return View();
        }

   
}

 

++++++++++++++++++++++++++++++++++++++++

The View Page

 

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<LinqToTwitter.Status>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>


    <% using (Html.BeginForm("Create","TweetController",FormMethod.Post, new {enctype="multipart/form-data"})){ %>
    <label>Tweet:</label> <%= Html.TextBox("userStatus") %>
        <input type='submit' value='updateStatus' />
        <label id="successLabel"></label>
<% } %>

  

   

   

</asp:Content>

Coordinator
Apr 19, 2011 at 6:12 PM

Hi,

You're close, the main thing you need to do is save he credentials after the call to auth.CompleteAuthorization() in Index.  Perhaps put them in Session state temporarily, but you can also save them in the DB because Twitter won't change them.  Then, in the Create action, retrieve the credentials, build a new MvcAuthorizer instance, and pass the MvcAuthorizer instance to a new instance of LinqToTwitter.  Remember that Web apps are stateless, meaning that every call to the controller creates a brand new controller instance that has no memory of any previous communications.  That's why you need to learn about things like Session state and reading and writing to the DB because you must manage the state yourself.  Statelessness is why the twitterCtx instance isn't doing anything for you because it's recreated on every request to the controller and the call to Create is for an object that is different than the previous call to Index.

Joe