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.


Messages In This Thread
Saving and Reading oAuth Tokens with TwitterVB - by brandonio21_phpbb3_import2 - 08-10-2011, 02:57 PM

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

Forum Jump:


Users browsing this thread: 1 Guest(s)