0

Hi i want to send private message with linqToTwitter, here is my code :

 var auth = new MvcAuthorizer
        {
            Credentials = new InMemoryCredentials()
            {
                ConsumerKey  = TwitterClient.ConsumerKey,
                ConsumerSecret = TwitterClient.ConsumerSecret,                    
                OAuthToken = TempData["AccessToken"] as string,
                AccessToken = TempData["TokenSecret"] as string
            }
        };            
        var twitterContext = new TwitterContext(auth);            
        var message = twitterContext.NewDirectMessage(auth.UserId, "Hi ucef, cool to discuss with you" + DateTime.Now);

but auth.UserId is null, have you any idea !!!

ucef
  • 557
  • 3
  • 10
  • 27
  • do you want send a DM to yourself? auth.UserId has the id of the user authenticated. and also, the access token and the token secret are valid? – Zach dev Oct 24 '12 at 21:09
  • thanks Mr Zach, you can see the link below , i describe how i get the accesstoken and token secret, my aim is to get the user id for user who was authenticated. i will send private message later. http://stackoverflow.com/questions/13050638/retreive-userid-and-send-private-message-with-linqtotwitter – ucef Oct 24 '12 at 21:55

1 Answers1

0

The Authorizer will only hold a UserID immediately after you go through the OAuth process, where the user authenticates your app. After the user authenticates your app, Twitter sends back the UserID and ScreenName.

You're loading all 4 credentials at the same time, which gives the convenience of being able to query Twitter, without going through the OAuth process. However, since you didn't go through the OAuth process, there isn't a way to populate the UserID.

One way to work around this is to save the UserID after the first time the user authenticates and retrieve the saved ID from where you stored it on subsequent queries.

Another work around is to do an Account VerifyCredentials query. The you can get the UserID from the Account.User.UserIdentifier property.

Joe

Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
  • i tried the both aproche,in the first, the auth.UserId is null, and the second it was exception 401, so i think think token and tokenSecret are not valid, can you Mr Joe verify the aproche which i get the token and tokenSecret is right, here is the link how i get it : http://stackoverflow.com/questions/13050638/retreive-userid-and-send-private-message-with-linqtotwitter. Thank you Mr Joe :-) !! – ucef Oct 25 '12 at 10:05
  • There's an MvcOauthDemo sample on this page: http://linqtotwitter.codeplex.com/wikipage?title=LINQ%20to%20Twitter%20Samples&referringTitle=Home - you might want to get the sample running first, which might help figure out the difference between your code and the sample. Also, I have a FAQ here to help debug 401 errors: http://linqtotwitter.codeplex.com/wikipage?title=LINQ%20to%20Twitter%20Samples&referringTitle=Home – Joe Mayo Oct 28 '12 at 01:24