Anonymous Anonymous - 2 months ago 180 0

No description

Vb.net

VB Twitter API Example

--------
App.Config
--------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <appSettings>
    <!-- Fill in your consumer key and secret here to make the OAuth sample work. -->
    <!-- Twitter sign-up: https://dev.twitter.com/ -->
    <add key="twitterConsumerKey" value="5eksjF9yEXMmb545DNmK23o2v"/>
    <add key="twitterConsumerSecret" value="mrtfUgvRW4hTUKXeue5LET7bTD7sn2puxXzJYDEHUDq7HdOAm1"/>
    <add key="twitterOAuthToken" value="720689202388680704-p6FJ5nPjY541bRQ1jgxbdBKpBmPErGK"/>
    <add key="twitterAccessToken" value="gkEI6O5U6Hgw2UAsVqmTvvue1ePj2DSrL51nFypX3QdkR"/>
  </appSettings>
</configuration>

--------
VB Code
--------
Imports LinqToTwitter
Imports System.Configuration

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Main()
    End Sub

    Sub Main()
        '
        ' get user credentials and instantiate TwitterContext
        '

        If String.IsNullOrEmpty(ConfigurationManager.AppSettings("twitterConsumerKey")) Or String.IsNullOrEmpty(ConfigurationManager.AppSettings("twitterConsumerSecret")) Then
            Console.WriteLine("Please set the Twitter consumer key and secret values in the app.config file and run again.")
            Exit Sub
        End If

        Dim credentials As New XAuthCredentials

        credentials.ConsumerKey = ConfigurationManager.AppSettings("twitterConsumerKey")
        credentials.ConsumerSecret = ConfigurationManager.AppSettings("twitterConsumerSecret")

        Dim auth As PinAuthorizer = New PinAuthorizer()
        auth.CredentialStore = credentials
        auth.GetPin = AddressOf VerifierCallback
        auth.GoToTwitterAuthorization = Function(pageLink) Process.Start(pageLink)
        auth.AuthorizeAsync()

        Dim twitterCtx As TwitterContext = New TwitterContext(auth)

        twitterCtx.Log = Console.Out

        'auth.SignOn()

        Dim twitterContext = New TwitterContext(auth)

        UserStatusByNameDemo(twitterCtx)

        Console.Read()
    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

    Private Sub UserStatusByNameDemo(ByVal twitterCtx As TwitterContext)
        Dim lastN As Integer = 20
        Dim screenName As String = "reecepeel_"

        Dim ts = From t In twitterCtx.Status
                 Where t.Type = StatusType.User _
                 And t.ScreenName = screenName _
                 And t.Count = lastN

        For Each tweet In ts
            txtTweets.Text = (
                "(" & tweet.ID & ")" &
                "[" & tweet.User.UserID & "]" &
                tweet.User.Name & ", " &
                tweet.Text & ", " &
                tweet.CreatedAt)
        Next
    End Sub

End Class
Comments