Before copy paste , read this explanation first Click HERE
To use this class , Read this
VB.NET 2010
'copy paste start here
Imports System.Security.Cryptography
Imports System.Text
Imports System.Net
Imports System.IO
Imports System.IO.Compression
Imports System.Windows.Forms
Public Class TwitterLib
Private Api_Response_Header As WebHeaderCollection
Private Base_Format As String
Private Method() As String = {"GET", "POST"}
Public chooseMethod As Integer = "0" 'User select 0(GET) or 1(POST)
Private oauth_signKey As String
Private oauth_baseString As String
Public oauth_consumer_key As String
Public oauth_consumer_secret As String
Private oauth_nonce As String = Convert.ToBase64String(New ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString())) & "AbCdEfKg" '32 bit string
Public oauth_resource_url As String
Private oauth_signature As String
Public oauth_signature_method As String = "HMAC-SHA1"
Private oauth_timestamp As String = (DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds
Public oauth_token As String
Public oauth_token_secret As String
Public oauth_version As String = "1.0"
Public oauth_host As String = "api.twitter.com"
Public count = "15"
Private workType() As String = {"Retrieval", "PostStatus"}
Public chooseWorkType As Integer = 0 'User select 0(Retrieval) or 1(PostStatus)
Public updateStatus As String = " "
Public limitation As String
Public rate_limit As String
Public utc_reset As String
Private _chars(,) As String = {{"%", "%25"}, {"$", "%24"}, {"&", "%26"}, {"+", "%2B"}, {",", "%2C"}, {"/", "%2F"}, {":", "%3A"}, {";", "%3B"}, {"=", "%3D"}, {"?", "%3F"}, {"@", "%40"}, {" ", "%20"}, {"\", "%22"}, {"<", "%3C"}, {">", "%3E"}, {"#", "%23"}, {"{", "%7B"}, {"}", "%7D"}, {"|", "%7C"}, {"\", "%5C"}, {"^", "%5E"}, {"~", "%7E"}, {"[", "%5B"}, {"]", "%5D"}, {"'", "%60"}}
Private Function Create_baseString(ByVal oauth_consumer_key As String, ByVal oauth_nonce As String, ByVal oauth_signature_method As String, ByVal oauth_timestamp As String, ByVal oauth_token As String, ByVal oauth_version As String) As String
Dim baseString As String
Try
Select Case workType(chooseWorkType)
Case "Retrieval"
Base_Format = "count={6}&oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}"
baseString = String.Format(Base_Format,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
count
)
Return String.Concat(Method(chooseMethod) & "&", Uri.EscapeDataString(oauth_resource_url), "&", Uri.EscapeDataString(baseString))
Case "PostStatus"
Base_Format = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}"
baseString = String.Format(Base_Format,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
Uri.EscapeDataString(updateStatus)
)
Return String.Concat(Method(chooseMethod) & "&", Uri.EscapeDataString(oauth_resource_url), "&", Uri.EscapeDataString(baseString))
Case Else
MessageBox.Show("Some parameter wrong or not complete please check your syntax !! (Er.Code : x01)")
Return "zero"
End Select
Catch ex As Exception
Return "error occured when creating basestring"
End Try
End Function
Private Function Create_signKey(ByVal oauth_consumer_secret As String, ByVal oauth_token_secret As String) As String
Try
Return Uri.EscapeDataString(oauth_consumer_secret) & "&" & Uri.EscapeDataString(oauth_token_secret)
Catch ex As Exception
Return "error occured when creating signkey"
End Try
End Function
Private Function Create_Signature(ByVal oauth_signKey As String, ByVal oauth_baseString As String) As String
Try
Dim encryption
encryption = New HMACSHA1(ASCIIEncoding.ASCII.GetBytes(oauth_signKey))
Using (encryption)
Return Uri.EscapeDataString(Convert.ToBase64String(encryption.ComputeHash(ASCIIEncoding.ASCII.GetBytes(oauth_baseString))))
End Using
Catch ex As Exception
Return "error occured when creating signature"
End Try
End Function
Private Sub generateParameter(ByRef oauth_baseString As String, ByRef oauth_signKey As String, ByRef oauth_signature As String)
Try
oauth_baseString = Create_baseString(oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_token, oauth_version)
oauth_signKey = Create_signKey(oauth_consumer_secret, oauth_token_secret)
oauth_signature = Create_Signature(oauth_signKey, oauth_baseString)
Catch ex As Exception
End Try
End Sub
Public Function EncodeText(ByVal url As String) As String
For i As Integer = 0 To _chars.GetUpperBound(0) - 1
url = url.Replace(_chars(i, 0), _chars(i, 1))
Next i
Return (url)
End Function
'Public Function Connect_Api_Twitter(ByVal oauth_resource_url As String, ByVal oauth_consumer_key As String, ByVal oauth_nonce As String, ByVal oauth_signature As String, ByVal oauth_signature_method As String, ByVal oauth_timestamp As String, ByVal oauth_token As String, ByVal oauth_version As String, ByRef Api_Response_Header As Object) As String
Public Function Connect_Api_Twitter() As String
Dim json As String
Dim reader As StreamReader
Dim myHttpWebResponse As HttpWebResponse
Dim myHttpWebRequest As HttpWebRequest
Dim myWebHeaderCollection As WebHeaderCollection
Dim responseStream As Stream
updateStatus = updateStatus.Replace("!", "")
Try
'Generate All Parameter
generateParameter(oauth_baseString, oauth_signKey, oauth_signature)
Select Case workType(chooseWorkType)
Case "Retrieval"
myHttpWebRequest = CType(WebRequest.Create(oauth_resource_url & "?count=" & count), HttpWebRequest)
Case "PostStatus"
myHttpWebRequest = CType(WebRequest.Create(oauth_resource_url & "?status=" & EncodeText(updateStatus)), HttpWebRequest)
Case Else
myHttpWebRequest = CType(WebRequest.Create(oauth_resource_url & "?count=" & count), HttpWebRequest)
End Select
myWebHeaderCollection = myHttpWebRequest.Headers
myHttpWebRequest.Method = Method(chooseMethod)
myHttpWebRequest.Host = oauth_host
'myHttpWebRequest.UserAgent = "Personal Application Learner"
myWebHeaderCollection.Add("Authorization: OAuth oauth_consumer_key=""" & oauth_consumer_key & """, oauth_nonce=""" & oauth_nonce & """, oauth_signature=""" & oauth_signature & """, oauth_signature_method=""" & oauth_signature_method & """, oauth_timestamp=""" & oauth_timestamp & """, oauth_token=""" & oauth_token & """, oauth_version=""" & oauth_version & """")
myWebHeaderCollection.Add("Accept-Encoding:gzip")
myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
responseStream = myHttpWebResponse.GetResponseStream()
If (myHttpWebResponse.ContentEncoding.ToLower().Contains("gzip")) Then
responseStream = New GZipStream(responseStream, CompressionMode.Decompress)
ElseIf (myHttpWebResponse.ContentEncoding.ToLower().Contains("deflate")) Then
responseStream = New DeflateStream(responseStream, CompressionMode.Decompress)
End If
reader = New StreamReader(responseStream, Encoding.Default)
json = reader.ReadToEnd()
Api_Response_Header = myHttpWebResponse.Headers
'Return Json and Header
If workType(chooseWorkType) = "PostStatus" Then
'if using for post twitter then no response header from server
Else
limitation = Api_Response_Header.GetValues(14)(0)
rate_limit = Api_Response_Header.GetValues(15)(0)
utc_reset = Api_Response_Header.GetValues(16)(0)
End If
Return json
Catch ex As Exception
MessageBox.Show("Error Occured, please check your connection or parameter")
json = "Error Occured please check the connection or parameter !!"
Return "error"
End Try
End Function
End Class
Post a Comment
Harap gunakan bahasa yang baik dan sopan, terima kasih