在vb.net中使用linqToTwitter

时间:2017-04-05 20:12:32

标签: vb.net twitter social-networking linq-to-twitter posting

我正在尝试添加从我的某个应用发布推文的功能。

我正在使用LinqToTwitter库并已阅读我似乎能找到的所有示例,但无法使其正常工作。有人能指出我正确的方向吗?

继承我的代码......

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

根据几个例子,我把它放在一起,从我可以说,它应该是正确的,但有一些错误......

  • 未定义“IOAuthCredentials”类型。
  • 未定义“InMemoryCredentials”类型。
  • 凭据不是Pin授权商的成员。
  • 'UpdateStatus'不是'TwitterContext'的成员。

周围几乎没有几个例子让我无处可去,我有点碰到砖墙。

任何人都能提供的任何帮助都非常适合。

1 个答案:

答案 0 :(得分:0)

这可能来自一个很老的例子。 LINQ to Twitter现在是异步的,旧的语法不起作用。以下是您应该如何重写以开展工作:

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

在Sub事件处理程序中使用Async原谅我的VB语法,但是如果你知道VB,你应该能够得到那个部分。为了以防万一,我上传了一个新的VB演示,以便您可以在控制台应用中看到它的工作原理:

LINQ to Twitter VB Sample

由于您提供了全部3个凭据,因此此代码应立即发送推文。但是,如果您只有前两个,GetPin和GoToTwitterAuthorization帮助指导用户授权过程。