简体   繁体   English

在vb.net中使用linqToTwitter

[英]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. 我正在使用LinqToTwitter库,并阅读了所有似乎可以找到但无法正常运行的示例。 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. 未定义类型“ IOAuthCredentials”。
  • Type 'InMemoryCredentials' is not defined. 未定义类型“ InMemoryCredentials”。
  • Credentials is not a member of Pin Authorizer. 凭据不是密码授权者的成员。
  • 'UpdateStatus' is not a member of 'TwitterContext'. 'UpdateStatus'不是'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. LINQ to Twitter现在是异步的,旧语法不起作用。 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. 用Sub事件处理程序上的Async赦免我的VB语法,但是如果您了解VB,则应该可以得到这一部分。 Just in case, I uploaded a new VB demo so you can see how this works in a Console app: 以防万一,我上传了一个新的VB演示,以便您可以在控制台应用程序中查看其工作方式:

LINQ to Twitter VB Sample LINQ to Twitter VB示例

Since you provided all 3 credentials, this code should tweet right away. 由于您提供了所有3个凭据,因此该代码应立即鸣叫。 However, if you only had the first two, the GetPin and GoToTwitterAuthorization help guide the user authorization process. 但是,如果只有前两个,则GetPin和GoToTwitterAuthorization帮助指导用户授权过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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