Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Twitter OAuth web request - post a Tweet

Status
Not open for further replies.
Feb 4, 2002
792
GB
Hi all,

Was wondering if anyone has any experience using OAuth - but NOT with one of the libraries available! All I want (or rather my boss wants) is to be able to send a tweet t=from a webpage. So all I need is code to build a web request, perhaps header too (or more than one?). Then send the request POST to Twitter API.

I can write code to make a POST. Just not clear how to interact with Twitter API.

I have read through API documentation, but it is unclear.

I have written some code that doesn't work, though compiles fine:
Code:
Public Shared Function Tweet(strText As String) As Boolean
        Dim boolResult As Boolean = False
        Dim urlAddress As Uri = New Uri("[URL unfurl="true"]https://api.twitter.com/1/statuses/update.json")[/URL]
        Dim strData As StringBuilder
        Dim byteData() As Byte
        Dim postStream As Stream = Nothing

        Dim strConsumerKey As String = "xxxxxx"
        Dim strConsumerSecret As String = "xxxxxx"
        Dim strAccessToken As String = "xxxxxx"
        Dim strAccessTokenSecret As String = "xxxxxx"

        Dim objRequest As HttpWebRequest
        Dim objResponse As HttpWebResponse = Nothing
        Dim objReader As StreamReader
        Dim objHeader As HttpRequestHeader = HttpRequestHeader.Authorization

        Try
            objRequest = DirectCast(WebRequest.Create(urlAddress), HttpWebRequest)

            objRequest.Method = "POST"
            objRequest.ContentType = "application/x-[URL unfurl="true"]www-form-urlencoded"[/URL]

            strData = New StringBuilder()
            strData.Append("&Hello_World%2521%3D%26oauth_consumer_key%3D" + strConsumerKey + "%26oauth_nonce%3Dda6bb8ce7e48547692f4854833afa680%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1329746260%26oauth_token%3D" + strAccessToken + "%26oauth_version%3D1.0")
            objRequest.Headers.Add(objHeader, "Authorization: OAuth oauth_consumer_key=""xxxx"", oauth_nonce=""da6bb8ce7e48547692f4854833afa680"", oauth_signature=""xxxx"", oauth_signature_method=""HMAC-SHA1"", oauth_timestamp=""1329750426"", oauth_token=""xxxx"", oauth_version=""1.0""")



            ' Create a byte array of the data we want to send  
            byteData = UTF8Encoding.UTF8.GetBytes(strData.ToString())

            ' Set the content length in the request headers  
            objRequest.ContentLength = byteData.Length

            Try
                postStream = objRequest.GetRequestStream()
                postStream.Write(byteData, 0, byteData.Length)
            Finally
                If Not postStream Is Nothing Then postStream.Close()
            End Try

            boolResult = True
        Catch ex As Exception
            boolResult = False
            HttpContext.Current.Session.Add("Error", ex.ToString())
        End Try

        Try
            ' Get response  
            objResponse = DirectCast(objRequest.GetResponse(), HttpWebResponse)

            ' Get the response stream into a reader  
            objReader = New StreamReader(objResponse.GetResponseStream())

            ' Console application output  
            Console.WriteLine(objReader.ReadToEnd())
        Finally
            If Not objResponse Is Nothing Then objResponse.Close()
        End Try

        Return boolResult
    End Function

Any help would be much appreciated!

Please assume I don't run the code as is above - the XXXXs are proper token and consumer keys from dev.twitter.com OAuth Tool. I also know that some of the request string bases might be redundant, as once I can see this work I will streamline the code. I just want it to work for now!! ;)

Will
 
Worked it out... Here is a link to a great article, including a comment post that shows my ported code. The article was in C#, but I ported to VB, then got it working.

Code Project

Will
[morning]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top