08-10-2011, 02:57 PM
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:
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:
This code might also prove useful.. It checks whether or not an oAuth Token for that username has already been saved.
I hope that this has helped!
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
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!