简体   繁体   中英

Using linqToTwitter in vb.net

I'm trying to add the ability to post tweets from one of my apps.

I'm using the LinqToTwitter library and have read all the examples i can seem to find, but cannot get it working. Can someone point me in the right direction?

Heres my code...

Imports LinqToTwitter
Imports LinqToTwitter

Public Class StatusUpdate

    Dim consumerkey As String = "n-BLAH-BLAH-BLAH-BLAH-VA"
    Dim ConsumerSecret As String = "5o-BLAH-BLAH-BLAH-BLAH-o"
    Dim OAuthToken As String = "8-BLAH-BLAH-BLAH-BLAH-tK"
    Dim AccessToken As String = "E6-BLAH-BLAH-BLAH-BLAH-8T"

    Public twitterctx As TwitterContext

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim credentials As IOAuthCredentials = New InMemoryCredentials
        credentials.ConsumerKey = consumerkey
        credentials.ConsumerSecret = ConsumerSecret
        credentials.OAuthToken = OAuthToken
        credentials.AccessToken = AccessToken
        Dim auth As PinAuthorizer = New PinAuthorizer()
        auth.Credentials = credentials
        Dim twitterCtx As TwitterContext = New TwitterContext(auth)
        twitterCtx.UpdateStatus(tweetbody.Text)

    End Sub
End Class

Thats what I've put together based on several examples and, from what I can tell, it should be right, but there are a few errors...

  • Type 'IOAuthCredentials' is not defined.
  • Type 'InMemoryCredentials' is not defined.
  • Credentials is not a member of Pin Authorizer.
  • 'UpdateStatus' is not a member of 'TwitterContext'.

The scarce few examples that are around are not getting me anywhere and I'm kinda hitting a brick wall.

Any help anyone can offer is greatly appriciated.

That's probably from a pretty old example. LINQ to Twitter is now async, and the old syntax doesn't work. Here's how you should re-write to make work:

Imports LinqToTwitter
Imports LinqToTwitter

Public Class StatusUpdate
    Dim consumerkey As String = "n-BLAH-BLAH-BLAH-BLAH-VA"
    Dim ConsumerSecret As String = "5o-BLAH-BLAH-BLAH-BLAH-o"
    Dim OAuthToken As String = "8-BLAH-BLAH-BLAH-BLAH-tK"
    Dim AccessToken As String = "E6-BLAH-BLAH-BLAH-BLAH-8T"

    Public twitterctx As TwitterContext

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim credentials As InMemoryCredentialStore = New InMemoryCredentialStore
        credentials.ConsumerKey = consumerkey
        credentials.ConsumerSecret = ConsumerSecret
        credentials.OAuthToken = OAuthToken
        credentials.AccessToken = AccessToken

        Dim auth As PinAuthorizer = New PinAuthorizer()
        auth.CredentialStore = credentials

        auth.GetPin = AddressOf VerifierCallback
        auth.GoToTwitterAuthorization = Function(pageLink) Process.Start(pageLink)

        Await auth.AuthorizeAsync()

        Dim twitterCtx As TwitterContext = New TwitterContext(auth)

        Await twitterCtx.TweetAsync(tweetbody.Text)

    End Sub

    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 Class

Pardon my VB syntax with Async on the Sub event handler, but if you know VB, you should be able to get that part. Just in case, I uploaded a new VB demo so you can see how this works in a Console app:

LINQ to Twitter VB Sample

Since you provided all 3 credentials, this code should tweet right away. However, if you only had the first two, the GetPin and GoToTwitterAuthorization help guide the user authorization process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM