how to authenticate through linq-to-twitter?

Nov 18, 2012 at 7:35 PM
Edited Nov 18, 2012 at 7:56 PM

Hello - I'm brand new to Linq-to-Twitter - just started using it today. I'm getting a TwitterQueryException ("Bad Authentication Data"). I guess I need to understand the InMemoryCredentials properties.  At dev.twitter.com the OAuth tool shows me the following OAuth Settings:

1. Consumer key
2. Consumer secret
3. Access token
4. Access token secret

Properties 1-3 of the Twitter OAuth tool OAuth Settings map directly to InMemoryCredentials properties. However, I'm not sure about the proper mapping of "Access token secret." I saw an example online that looked something like the following: 

var authorize = new PinAuthorizer           
{                
Credentials = new InMemoryCredentials                
{                   
ConsumerKey = "9o0coGkh2zEjGFalkeXXX",                    
ConsumerSecret = "ZkEiPnm6atI8iuiJ7xkZne5wam3Q0uH0KZ2svJiXXX",             
AccessToken = "50993680-t5yF23ct3vJCaFwS0U5a8KuwdomezGd2dPlHGXXX",                        
OAuthToken = "xKXvNVkswCbuJSpdKMqrbQOZsgq7mx8Bs218F03XXX"             
}            
};

I'm currently using my "Access token secret" value from the Twitter OAuth tool for the InMemoryCredentials OAuthToken property value but I'm not sure if that's correct or if that may be causing my issue.  Here's the code where I pass the authorize token and API urs into the TwitterContext:

var twitterCtx = new TwitterContext(authorize, "https://api.twitter.com/1.1/", "https://api.twitter.com/1.1/statuses/user_timeline.json");

So am I misunderstanding the mapping of the InMemoryCredentials OAuthToken property? How can I fix my "Bad Authentication Data" error?

Coordinator
Nov 18, 2012 at 7:52 PM

Hi,

You would fill in all 4 credentials if you wanted LINQ to Twitter to run as a single user all the time. In that case, the config file OAuthToken is the Twitter AccessToken and the config file AccessToken is the Twitter AccessTokenSecret. BTW, you can rename config file entries and change the names in your code to match.

The other option is to have the user perform authentication. In that case, leave the AccessToken and OAuthToken empty and LINQ to Twitter will initiate the OAuth authentication process with Twitter. After authentication, you can read the Authorizer Credentials property to get credentials for that user and save those credentials (in a database) for the user's subsequent queries. Twitter doesn't change credentials, so you can reuse those same credentials and not make the user authenticate anymore after the first time.

All you need to instantiate TwitterContext is the constructor overload that accepts an authorizer:

           var twitterCtx = new TwitterContext(authorize);

At one time, Twitter had different URLs for different parts of its API, which changed occasionally. Now that LINQ to Twitter supports the new Twitter API v1.1, the url overloads aren't necessary.

Here's some documentation on how OAuth works with LINQ to Twitter:

Securing Your Applications

 BTW, you'll want to edit and remove credentials from your post above so noone steals them. Better yet, visit your application at dev.twitter.com and generate a new set of keys. Those keys allow anyone to perform actions on Twitter on your behalf and right now you're pretty insecure.

@JoeMayo