BP Forums
Saving and Reading oAuth Tokens with TwitterVB - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+----- Forum: Code Snippets (https://bpforums.info/forumdisplay.php?fid=18)
+----- Thread: Saving and Reading oAuth Tokens with TwitterVB (/showthread.php?tid=387)



Saving and Reading oAuth Tokens with TwitterVB - brandonio21 - 08-10-2011

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!


Re: Saving and Reading oAuth Tokens with TwitterVB - jaydude28 - 08-11-2011

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.


Re: Saving and Reading oAuth Tokens with TwitterVB - brandonio21 - 08-11-2011

Please put the whole project into a zip file, and I can help you from there.


Re: Saving and Reading oAuth Tokens with TwitterVB - jaydude28 - 08-12-2011

Attached is the entire project zipped. Thanks again.


Re: Saving and Reading oAuth Tokens with TwitterVB - brandonio21 - 08-13-2011

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]


Re: Saving and Reading oAuth Tokens with TwitterVB - jaydude28 - 08-14-2011

Thanks for your help. I'm looking over the code to understand it.


Re: Saving and Reading oAuth Tokens with TwitterVB - brandonio21 - 08-14-2011

It is fairly simple code, but if you have any questions, feel free to ask!