Linq2Twitter and Asp.Net OpenAuth

Nov 15, 2013 at 12:22 AM
I am using Linq2Twitter in web forms and it works great except that I really want to keep the Asp.Net Login framework in place since it handles the local membership issues nicely for me.

In the VS 2012 template it uses the OpenAuth object and in RegisterExternalLogin there is this:
  // Verify the OpenAuth payload
  var authResult = OpenAuth.VerifyAuthentication(redirectUrl);
authResult includes a returned AccessToken which I thought was all I would need to make subsequent call on behalf of a user, like this, to subscribe the current logged in user to a List:

(where 'data' contains the previously saved authResult.ExtraData["accesstoken"] string)
  UsersOpenAuthData data = (from d in sm.DataContext.UsersOpenAuthDatas 
                                            where d.MembershipUserName == User.Identity.Name 
                                            select d).SingleOrDefault();

//Get WebAuth
SessionStateCredentials cred = new SessionStateCredentials
{
           ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
           ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"],
            AccessToken = data.AccessToken
};
WebAuthorizer wa = new WebAuthorizer
{
           Credentials = cred,
           PerformRedirect = authUrl => Response.Redirect(authUrl)
};
TwitterContext twitterCtx = new TwitterContext(wa);
List list = twitterCtx.AddMemberToList(null, MyAccountName, MyListID, null, null, null);
and here I get a 401, I suspect because Credentials wants an OAuthToken value separately from the AccessToken value ?

I know that my App is OK and my config Consumer values are correct as I am able to use them to log in / register elsewhere.

Can this be made to work or do I need to have L2T all the way through in order to get both tokens?
Coordinator
Nov 15, 2013 at 4:21 AM
Hi,

There isn't any integration between in LINQ to Twitter and MVC OAuth support. I haven't looked into this particular scenario yet. Right now, most of my LINQ to Twitter development time is dedicated towards async and PCL support in v3.0.

Follow @JoeMayo
Nov 15, 2013 at 2:33 PM
The answer appears to be to replace the .Net calls to
 OpenAuth.VerifyAuthentication(redirectUrl);
and
var authResult = OpenAuth.VerifyAuthentication(redirectUrl);
with the corresponding Linq2Twitter calls
  /* use L2T instead*/
_credentials = new SessionStateCredentials{
     ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
     ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]
};
_auth = new WebAuthorizer
{
     Credentials = _credentials,
     PerformRedirect = authUrl => Response.Redirect(authUrl)
};
redirectUrl = "http://" + Request.Url.Authority + ResolveUrl(redirectUrl);
Uri returnURI = new Uri(ResolveUrl(redirectUrl));
_auth.BeginAuthorization(returnURI);
and on return:
_credentials = new SessionStateCredentials
            {
                ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]
            };

            _auth = new WebAuthorizer
            {
                Credentials = _credentials,
                PerformRedirect = authUrl => Response.Redirect(authUrl)
            };
            _auth.CompleteAuthorization(Request.Url);
Marked as answer by Serexx on 11/15/2013 at 9:31 AM
Nov 15, 2013 at 2:57 PM
Edited Nov 15, 2013 at 2:58 PM
Hi Joe - thanks, my original post was a bit much and I (and others) do appreciate all the work you are doing on Linq2Twitter. As an aside please define "PCL support" (more to learn!).

Hoping I can squeeze in a more straightforward Question:

I have replaced the OpenAuth calls (see my separate note) here & things appear to work correctly - I do get the WebAuthorizer object back with data

On subsequent calls for the user though I continue to get the ubiquitous 401 so I am going through the 401 checklist you posted (thank you!) but I also want to be sure that I am grabbing the right tokens when first authenticating the user.
_auth.CompleteAuthorization(Request.Url);

myOAuthToken = _auth.Credentials.OAuthToken;
muAccessToken = _auth.Credentials.AccessToken;

//or

myOAuthToken = _auth.OAuthTwitter.OAuthToken;
muAccessToken = _auth.OAuthTwitter.OAuthTokenSecret;

// ??
in my app they seem to be identical but I am not clear that they always will be the case, or perhaps should not be!

Am I grabbing the right data?
Coordinator
Nov 15, 2013 at 5:05 PM
The proper way to access credentials is via your first scenario via _auth.Credentials.

@JoeMayo
Coordinator
Nov 15, 2013 at 8:51 PM
PCL == Portable Class Libraries. More info here:

http://msdn.microsoft.com/en-us/library/vstudio/gg597391(v=vs.110).aspx

The benefit of this in developing LINQ to Twitter is that it makes the code simpler by having one platform target. It's easier and faster to fix bugs and add new features. In addition to development, it's easier to deploy one DLL to multiple platforms. e.g. I just got this working on Xamarin.Android:

http://geekswithblogs.net/WinAZ/archive/2013/11/14/linq-to-twitter-runs-xamarin.android.aspx

@JoeMayo