Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving and Reading oAuth Tokens with TwitterVB
#1
I have received a request to post a code that would allow the user to save and read the user's Twitter oAuth Tokens, so here it is.

This is the code that writes the file:
Code:
Public Shared Function getOAuth(ByVal username As String, ByVal PIN As String)
        Try
            Dim writer As New System.IO.StreamWriter(My.Application.Info.DirectoryPath + "/" + username + ".dat")
            Dim pinIsValid As Boolean = tw.ValidatePIN(PIN)
            If pinIsValid = True Then
                'PIN is valid, write it
                Dim oAuth As String = tw.OAuth_Token
                Dim oAuthSecret As String = tw.OAuth_TokenSecret
                writer.Write(oAuth + "/" + oAuthSecret)
                writer.Close()
                Return "oAuth Information Written!"
            Else
                Return "Invalid PIN"
            End If
        Catch ex As Exception
            Trace.TraceWarning("Error while trying to get oAuth Tokens, " + ex.Message)
            Return ""
        End Try
    End Function
Keep in mind that code also checks to see whether the PIN is valid or not, so you could use this entire code snippet to validate the twitter username.

This is the code to authenticate the user:
Code:
Public Shared Function Authenticate(ByVal username As String) As Boolean
        Try
            Dim reader As New System.IO.StreamReader(My.Application.Info.DirectoryPath + "/" + username + ".dat")
            Dim oAuthToken As String = reader.ReadToEnd.Split("/")(0).ToString
            Dim oAuthTokenSecret As String = reader.ReadToEnd.Split("/")(1).ToString
            reader.Close()
            tw.AuthenticateWith(consumerKey, consumerSecret, oAuthToken, oAuthTokenSecret)
            Return True
        Catch ex As Exception
            Trace.TraceWarning("There was an error while trying to authenticate, " + ex.Message)
            Return False
        End Try
    End Function

This code might also prove useful.. It checks whether or not an oAuth Token for that username has already been saved.

Code:
Public Shared Function oAuthExists(ByVal username As String) As Boolean
        Try
            If My.Computer.FileSystem.FileExists(My.Application.Info.DirectoryPath + "/" + username + ".dat") Then
                Dim reader As New System.IO.StreamReader(My.Application.Info.DirectoryPath + "/" + username + ".dat")
                If reader.ReadToEnd = "" Then
                    Return False
                Else
                    Return True
                End If
                reader.Close()
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
    End Function

I hope that this has helped!
My Blog | My Setup | My Videos | Have a wonderful day.
#2
Thanks for the sample code. I'm a little bit lost in how to add/implement it into my form. I have attached my form in hopes that you could help. Thanks.


Attached Files
.zip   Form1.zip (Size: 1.26 KB / Downloads: 670)
#3
Please put the whole project into a zip file, and I can help you from there.
My Blog | My Setup | My Videos | Have a wonderful day.
#4
Attached is the entire project zipped. Thanks again.


Attached Files
.zip   NerdyTweet.zip (Size: 1.4 MB / Downloads: 649)
#5
Okay, so I have made it so you only have to type in the code once... You'll see what I mean if you take a look at the code.

[attachment=0]<!-- ia0 -->NerdyTweet.zip<!-- ia0 -->[/attachment]


Attached Files
.zip   NerdyTweet.zip (Size: 1.41 MB / Downloads: 685)
My Blog | My Setup | My Videos | Have a wonderful day.
#6
Thanks for your help. I'm looking over the code to understand it.
#7
It is fairly simple code, but if you have any questions, feel free to ask!
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading and Wrinting XML Files Snake_eyes 1 9,274 11-17-2012, 01:52 PM
Last Post: brandonio21
  Retrieving the source of a tweet using TwitterVB brandonio21 4 15,261 08-09-2011, 02:43 PM
Last Post: jaydude28

Forum Jump:


Users browsing this thread: 1 Guest(s)