I've identified the issue and my PIN authentication is now working
correctly.  I'll do my best to explain the issue here to save others
the headaches I've had. :)

The OAuth class that Eran Sandler so graciously provided to the
community (http://oauth.googlecode.com/svn/code/csharp/OAuthBase.cs)
contains a method called "GetQueryParameters()".  GetQueryParameters
is used to build the query string of any OAuth web request.  If you
look at the code of that method, you'll see a foreach() loop that
contains the following IF statement:

if (!string.IsNullOrEmpty(s) && !s.StartsWith(OAuthParameterPrefix)) {

The effect of this code is that querystring parameters are only added
to the web request if they DO NOT start with "oauth_".  So, when I was
trying to create a web request with "oauth_verifier" on the
querystring, Eran's code was stripping it out.  Without that item on
the query string, Twitter was (quite rightly) telling me to get lost.

My immediate solution was to remove the "&& !s.StartsWith
(OAuthParameterPrefix)" from the above line.  This allowed the rest of
the OAuth class (and Shannon Whitley's extension class) to process my
PIN request correctly.

I understand that this may not be the BEST solution, and I would
greatly appreciate hearing better ones.

On Jun 29, 10:18 am, Matt Sanford <[email protected]> wrote:
> Hi Duane,
>
>      When you get the 401 what does the body say?
>
> Thanks;
>   – Matt Sanford / @mzsanford
>       Twitter Dev
>
> On Jun 29, 2009, at 4:29 AM, DWRoelands wrote:
>
>
>
> > I'm having a devil of a time getting my application to connect to
> > Twitter via OAuth and the six-digit pin method.
>
> > I've been working with the excellent OAuth library from
> > ShannonWhitley:
> >http://www.voiceoftech.com/swhitley/?p=681
>
> > I'm able to get an authorization link, open a browser, and get the  
> > six-
> > digit PIN.  However, I can't seem to submit that PIN back to Twitter
> > without getting a 401 Unauthorized error.
>
> > I added this method to Shannon's TWitter OAuth code:
>
> >        public bool ValidatePin(string PIN)
> >        {
> >            string response = WebRequest(Method.GET, string.Format
> > ("{0}?oauth_verifier={1}", ACCESS_TOKEN, PIN), string.Empty);
> >            if (response.Length > 0)
> >            {
> >                //Store the Token and Token Secret
> >                var qs = HttpUtility.ParseQueryString(response);
> >                if (!string.IsNullOrEmpty(qs["oauth_token"]))
> > this.Token = qs["oauth_token"];
> >                if (!string.IsNullOrEmpty(qs["oauth_token_secret"]))
> > this.TokenSecret = qs["oauth_token_secret"];
> >                return true;
> >            }
> >            else
> >            {
> >                return false;
> >            }
> >        }
>
> > Inevitably, I get 401 Unauthorized when submitting the
> > "oauth_verifier" web request.
> > I have a consumer key and a consumer key secret which work (as is
> > evidenced by the fact that I can get an authorization link).
>
> > Has anyone else encountered similar issues, or does anyone else have
> > any guidance on this particular issue?
>
> > Thanks in advance.
> > --Duane

Reply via email to