SittingbourneMe Zoe,
I too have had to rush to get this working again, for an app in our case was using twitterizer previously.
I’ve broken this down into a few classes to abstract away a lot of the code and parse the response. Admittedly I’ve rushed a bit.
Classes:
OAuth for authentication
GETRequest to send the web request
TimeLine & Tweet - Data Structures
TimeLineParser - Changes the response text into a timeline object.
oAuth:
Option Strict On
Imports System.IO
Imports System.Text
Imports System.Net
Imports System.Security.Cryptography
Namespace SocialNetworking.Twitter
Public Class OAuth
#Region "Members"
Private m_Token As String = ""
Private m_TokenSecret As String = ""
Private m_ConsumerKey As String = ""
Private m_ConsumerSecret As String = “”
Private m_Version As String = "1.0"
Private m_SignatureMethod As String = "HMAC-SHA1"
Private m_Signature As String = ""
Private m_Nonce As String = ""
Private m_TimeSpan As TimeSpan = Nothing
Private m_Timestamp As String = ""
Private m_ScreenName As String = ""
Private m_ResourceURL As String = ""
#End Region
#Region "Properties"
Public Property Token() As String
Get
Return m_Token
End Get
Set(value As String)
m_Token = value
End Set
End Property
Public Property TokenSecret() As String
Get
Return m_TokenSecret
End Get
Set(value As String)
m_TokenSecret = value
End Set
End Property
Public Property ConsumerKey() As String
Get
Return m_ConsumerKey
End Get
Set(value As String)
m_ConsumerKey = value
End Set
End Property
Public Property ConsumerSecret() As String
Get
Return m_ConsumerSecret
End Get
Set(value As String)
m_ConsumerSecret = value
End Set
End Property
Public Property ScreenName() As String
Get
Return m_ScreenName
End Get
Set(value As String)
m_ScreenName = value
End Set
End Property
Public Property ResourceURL() As String
Get
Return m_ResourceURL
End Get
Set(value As String)
m_ResourceURL = value
End Set
End Property
#End Region
#Region “Constructors”
Private Sub CommonNew()
m_Nonce = Convert.ToBase64String(New ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()))
m_TimeSpan = DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)
m_Timestamp = Convert.ToInt64(m_TimeSpan.TotalSeconds).ToString()
End Sub
Public Sub New()
CommonNew()
End Sub
Public Sub New(ByVal ScreenName As String)
CommonNew()
m_ScreenName = ScreenName
End Sub
Public Sub New(ByVal ScreenName As String, ByVal ResourceURL As String)
CommonNew()
m_ScreenName = ScreenName
m_ResourceURL = ResourceURL
End Sub
#End Region
Private Sub CreateToken()
Dim baseFormat As String = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + _
"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}"
Dim baseString As String = String.Format(baseFormat,
m_ConsumerKey,
m_Nonce,
m_SignatureMethod,
m_Timestamp,
m_Token,
m_Version,
Uri.EscapeDataString(m_ScreenName))
baseString = String.Concat("GET&", Uri.EscapeDataString(m_ResourceURL), "&", Uri.EscapeDataString(baseString))
Dim compositeKey As String = String.Concat(Uri.EscapeDataString(m_ConsumerSecret), "&", Uri.EscapeDataString(m_TokenSecret))
Dim hasher As New HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))
m_Signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)))
'hasher.Dispose()
End Sub
Public Function GetHeader() As String
CreateToken()
' create the request header
Dim headerFormat As String = "OAuth oauth_nonce=""{0}"", oauth_signature_method=""{1}"", " + _
"oauth_timestamp=""{2}"", oauth_consumer_key=""{3}"", " + _
"oauth_token=""{4}"", oauth_signature=""{5}"", " + _
"oauth_version=""{6}"""
Dim authHeader As String = String.Format(headerFormat, _
Uri.EscapeDataString(m_Nonce), _
Uri.EscapeDataString(m_SignatureMethod), _
Uri.EscapeDataString(m_Timestamp), _
Uri.EscapeDataString(m_ConsumerKey), _
Uri.EscapeDataString(m_Token), _
Uri.EscapeDataString(m_Signature), _
Uri.EscapeDataString(m_Version))
Return authHeader
End Function
End Class
End Namespace
GetRequest
Option Strict On
Imports System.Net
Imports System.IO
Namespace SocialNetworking.Twitter
Public Class GETRequest
#Region “Members”
Private m_ResourceUrl As String = ""
Private m_Authenticate As Boolean = True
Private m_AuthHeader As String = ""
Private m_RequestMethod As String = "GET"
Private m_ContentType As String = "text/plain" '"application/x-www-form-urlencoded"
Private m_ScreenName As String = ""
Private m_OAuth As OAuth = Nothing
#End Region
#Region "Properties"
Public Property OAuth() As OAuth
Get
Return m_OAuth
End Get
Set(value As OAuth)
m_OAuth = value
End Set
End Property
Public Property ScreenName() As String
Get
Return m_ScreenName
End Get
Set(value As String)
m_ScreenName = value
End Set
End Property
#End Region
Public Sub New(ByVal ResourceURL As String)
m_ResourceUrl = ResourceURL
End Sub
Public Sub New(ByVal ResourceURL As String, ByVal Authenticate As Boolean)
m_ResourceUrl = ResourceURL
m_Authenticate = Authenticate
End Sub
Public Sub New(ByVal ResourceURL As String, ByVal Authenticate As Boolean, OAuth As OAuth)
m_ResourceUrl = ResourceURL
m_Authenticate = Authenticate
m_OAuth = OAuth
End Sub
Public Function GETResponse() As String
ServicePointManager.Expect100Continue = False
Dim postBody As String = "screen_name=" + Uri.EscapeDataString(m_ScreenName)
m_ResourceUrl += "?" + postBody
Dim request As HttpWebRequest = CType(WebRequest.Create(m_ResourceUrl), HttpWebRequest)
If m_Authenticate Then
m_AuthHeader = m_OAuth.GetHeader()
request.Headers.Add("Authorization", m_AuthHeader)
End If
request.Method = m_RequestMethod
request.ContentType = m_ContentType
Dim response As WebResponse = request.GetResponse()
Dim responseData As String = New StreamReader(response.GetResponseStream()).ReadToEnd()
Return responseData
End Function
End Class
End Namespace
Tweet
Option Strict On
Namespace SocialNetworking.Twitter
Public Class Tweet
Private m_DateTime As Date
Private m_Text As String
Public Property DateTime() As Date
Get
Return m_DateTime
End Get
Set(value As Date)
m_DateTime = value
End Set
End Property
Public Property Text As String
Get
Return m_Text
End Get
Set(value As String)
m_Text = value
End Set
End Property
End Class
End Namespace
TimeLine
Option Strict On
Namespace SocialNetworking.Twitter
Public Class TimeLine
Private m_TimeLine As New List(Of Tweet)
Public Property TimeLine() As List(Of Tweet)
Get
Return m_TimeLine
End Get
Set(value As List(Of Tweet))
m_TimeLine = value
End Set
End Property
End Class
End Namespace
TimeLineParser
Option Strict On
Namespace SocialNetworking.Twitter
Public Class TimeLineParser
Private m_Gumf As String = ""
Private m_TimeLine As TimeLine = New TimeLine()
Public Property Gumf() As String
Get
Return m_Gumf
End Get
Set(value As String)
m_Gumf = value
ProcessGumf()
End Set
End Property
Public Property TimeLine() As TimeLine
Get
Return m_TimeLine
End Get
Set(value As TimeLine)
m_TimeLine = value
End Set
End Property
Private Sub ProcessGumf()
Dim timelineParts As String() = m_Gumf.Split({"{""created_at"":"}, StringSplitOptions.RemoveEmptyEntries)
For i As Integer = 1 To timelineParts.Length - 1 'miss first out
Dim fullTweet As String = timelineParts(i)
Dim tweetParts As String() = fullTweet.Split({""","""}, StringSplitOptions.None)
Dim textPart As String = tweetParts(2).Replace("text"":""", "").Split({" http"}, StringSplitOptions.None)(0)
Dim timePart As String = tweetParts(0).Replace("""", "")
Try
Dim year As Integer = CInt(timePart.Split(CChar(" "))(5))
Dim day As Integer = CInt(timePart.Split(CChar(" "))(2))
Dim monthName As String = timePart.Split(CChar(" "))(1)
Dim month As Integer
Select Case monthName.ToUpper()
Case "JAN"
month = 1
Case "FEB"
month = 2
Case "MAR"
month = 3
Case "APR"
month = 4
Case "MAY"
month = 5
Case "JUN"
month = 6
Case "JUL"
month = 7
Case "AUG"
month = 8
Case "SEP"
month = 9
Case "OCT"
month = 10
Case "NOV"
month = 11
Case "DEC"
month = 12
End Select
Dim hour As Integer
Dim minute As Integer
Dim second As Integer
Dim hhmmss As String = timePart.Split(CChar(" "))(3)
hour = CInt(hhmmss.Split(CChar(":"))(0))
minute = CInt(hhmmss.Split(CChar(":"))(1))
second = CInt(hhmmss.Split(CChar(":"))(2))
Dim tweetDate As Date = New Date(year, month, day, hour, minute, second)
Dim newTweet As New Tweet()
With newTweet
.DateTime = tweetDate
.Text = System.Web.HttpUtility.HtmlDecode(textPart).Replace("\u2019", "'").Replace("\u00a0", " ")
End With
TimeLine.TimeLine.Add(newTweet)
Catch ex As Exception
Throw ex
End Try
Next
End Sub
End Class
End Namespace
Example Consuming Code:
''Authentication rubbish!
Dim ao As New OAuth("", “https://api.twitter.com/1.1/statuses/user_timeline.json”)
ao.Token = ""
ao.TokenSecret = ""
ao.ConsumerKey = ""
ao.ConsumerSecret = “”
Dim ge As New GETRequest("https://api.twitter.com/1.1/statuses/user_timeline.json", True, ao)
ge.ScreenName = "<REPLACE>"
Dim p As TimeLineParser = New TimeLineParser
p.Gumf = ge.GETResponse()
TextBox1.Clear()
For i As Integer = 0 To 2 'first three tweets
Try
TextBox1.Text += vbCrLf + vbCrLf + vbCrLf
TextBox1.Text += p.TimeLine.TimeLine(i).DateTime.ToString()
TextBox1.Text += vbCrLf + vbCrLf
TextBox1.Text += p.TimeLine.TimeLine(i).Text
Catch ex As Exception
throw ex
End Try
Next
You may need to add further properties to the tweet, and refine the parser to get them. Hope it helps.
Al