How can I get mentions with basic auth

Aug 20, 2009 at 2:06 PM

Hi.

I can't get mentions status with basic auth. My code is the following. Probably, it gets public timeline.

Dim auth = New UsernamePasswordAuthorization With {.UserName = "*id*", .Password = "*pass*"}
auth.SignOn()
Dim twitterContext = New TwitterContext(auth)
Dim ts = From t In twitterContext.Status _
         Where t.Type = StatusType.Mentions

Authentication is successful. Because UpdateStatus method is work fine. How can I get mentions?

 

Coordinator
Aug 20, 2009 at 4:11 PM

Hi,

Here's a demo that is working for me:

Imports System.Configuration
Imports LinqToTwitter

Module Module1

    Sub Main()
        '
        ' get user credentials and instantiate TwitterContext
        '
        Dim auth As ITwitterAuthorization

        If String.IsNullOrEmpty(ConfigurationManager.AppSettings("twitterConsumerKey")) Or String.IsNullOrEmpty(ConfigurationManager.AppSettings("twitterConsumerSecret")) Then
            Console.WriteLine("Skipping OAuth authorization demo because twitterConsumerKey and/or twitterConsumerSecret are not set in your .config file.")
            Console.WriteLine("Using username/password authorization instead.")

            ' For username/password authorization demo...
            auth = New UsernamePasswordAuthorization(Utilities.GetConsoleHWnd())
        Else
            Console.WriteLine("Discovered Twitter OAuth consumer key in .config file.  Using OAuth authorization.")

            ' For OAuth authorization demo...
            auth = New DesktopOAuthAuthorization()
        End If

        Dim twitterCtx As TwitterContext
        twitterCtx = New TwitterContext(auth, "https://twitter.com/", "http://search.twitter.com/")

        If twitterCtx.AuthorizedClient.GetType() Is GetType(OAuthAuthorization) Then
            InitializeOAuthConsumerStrings(twitterCtx)
        End If
        auth.SignOn()

        Dim twitterContext = New TwitterContext(auth)
        Dim ts = From t In twitterContext.Status _
                 Where t.Type = StatusType.Mentions

        For Each mention In ts
            Console.WriteLine("Mention: " & mention.Text)
        Next

        Console.ReadKey()
    End Sub

    Private Sub InitializeOAuthConsumerStrings(ByVal twitterCtx As TwitterContext)
        Dim oauth As DesktopOAuthAuthorization
        oauth = CType(twitterCtx.AuthorizedClient, DesktopOAuthAuthorization)
        oauth.GetVerifier = AddressOf VerifierCallback
    End Sub

    Private Function VerifierCallback() As String
        Console.WriteLine("Next, you'll need to tell Twitter to authorize access.\nThis program will not have access to your credentials, which is the benefit of OAuth.\nOnce you log into Twitter and give this program permission,\n come back to this console.")
        Console.Write("Please enter the PIN that Twitter gives you after authorizing this client: ")
        Return Console.ReadLine()
    End Function
End Module

This is a conversion to VB from the C# demo code in Program.cs. Hope it helps.

Joe

 

Aug 20, 2009 at 4:47 PM

Thank you for your quick reply.

Did you use username/password authorization? I can't get mentions using the code.

 

 

Coordinator
Aug 20, 2009 at 5:55 PM

This line of code prompts for username and password:

auth = New UsernamePasswordAuthorization(Utilities.GetConsoleHWnd())

The line of code above will execute if you don't have OAuth parameters defined in your app.config.  I didn't get it to work the way you were using it.  I did notice that not every query worked because I received occasional errors from Twitter, rejecting my credentials.  However, I tried multiple times with different accounts and it eventually worked.  Because of their recent episodes with DDOS attacks, it might be that there is an extended time-out for incorrect logins, causing you to wait before trying again. 

Joe

Aug 21, 2009 at 2:07 PM

Thank you for your reply.

I tried multiple times and another accounts. However, I can't get mentions.

And I analyzed LINQ to twitter library. As a result, this code doesn't work fine using VB(it works fine using C#.) VB and C# compiler create different Expression objects from LINQ statement.

Test codes are the following,

---

VB

 

Sub Main()
    Dim auth = New UsernamePasswordAuthorization
    Dim twitterContext = New TwitterContext(auth)
    auth.SignOn()
    Dim ts = From t In twitterContext.Status _
             Where t.Type = StatusType.Mentions _
             AndAlso t.Count = 20

    For Each mention In ts
        Console.WriteLine("Mention: " & mention.Text)
    Next
    Console.ReadKey()
End Sub

C#

 

 

static void Main(string[] args)
{
    var auth = new UsernamePasswordAuthorization();
    var twitterContext = new TwitterContext(auth);
    auth.SignOn();
    var ts = from t in twitterContext.Status
             where t.Type == StatusType.Mentions && t.Count == 20 
             select t;

    foreach (var mention in ts)
    {
        Console.WriteLine("Mention: " + mention.Text);
    }
    Console.ReadKey();
}

---

 

StatusRequestProcessor.cs in the Library, about 43 lines:

 

var parameters = paramFinder.Parameters;

 

In case of VB, this parameters variable's(Dictionary object) Count property is 1. This parameters has ["Count", "20"] pair only , not have "Type" key. In case of C#, this has "Count" and "Type" keys.

Because of VB compiler created expression that is "{t => ((ConvertChecked(t.Type) = 4) && (t.Count = 20))} C# compiler created "{t => ((Convert(t.Type) = 4) && (t.Count = 20))}"

Probably, to fix this problem you can support ConvertChecked in ExpressionTreeHelper class. And I want it!

Thank you.

Aug 22, 2009 at 4:12 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Aug 23, 2009 at 6:30 AM

Hi 5zj,

Thanks for your persistence.  I wasn't seeing the error when I ran the code, but (after looking again) it was clearly pulling up public statuses instead of mentions.  Your analysis was right on too.  I went into ExpressionTreeHelpers and found the problem right away.  I've checked in the fix here:

http://linqtotwitter.codeplex.com/SourceControl/changeset/view/58325

Could you test this and verify that it works?

Joe

 

Aug 23, 2009 at 3:52 PM

Hi Joe,

I checked for the code using VB and new Library(Change set 58325). And it works fine! In case of C#, it works fine too. Thank you.