Yorkiebar14 0 Junior Poster in Training

Hello,

I am trying to create a simple application for personal use for Twitter marketing. This is my first time trying to interact with REST APIs through VB and I have been going around and round getting various different errors.

Here is my current code and I get a '401 Unauthorised' error;

'---------------------------

TwitterApp

System.Net.WebException: The remote server returned an error: (401) Unauthorized.

at System.Net.HttpWebRequest.GetResponse()

at TwitterApp.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\User\AppData\Local\Temporary Projects\TwitterApp\Form1.vb:line 50
OK

'

Here's my code, no idea how right it is...

`Imports System.Net
Imports System.IO
Imports System.Text

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim reader As StreamReader
    Dim address As Uri
    Dim data As StringBuilder
    Dim byteData() As Byte
    Dim postStream As Stream = Nothing

    ' Set the REST API URL
    address = New Uri("https://api.twitter.com/oauth/request_token")
    ' Create the web request
    request = DirectCast(WebRequest.Create(address), HttpWebRequest)

    ' Set type to POST  
    request.Method = "POST"
    request.ContentType = "application/x-www-form-urlencoded"

    ' Create the data we want to send (each data.Append is for specific paramater data) 
    data = New StringBuilder()
    data.Append("oauth_callback=" + WebUtility.UrlEncode("http%3A%2F%2Flocalhost%2Fsign-in-with-twitter%2F"))
    data.Append("&oauth_consumer_key=" + WebUtility.UrlEncode("cChZNFj6T5R0TigYB9yd1w"))
    data.Append("&oauth_nonce=" + WebUtility.UrlEncode("ea9ec8429b68d6b77cd5600adbbb0456"))
    data.Append("&oauth_signature=" + WebUtility.UrlEncode("F1Li3tvehgcraF8DMJ7OyxO4w9Y%3D"))
    data.Append("&oauth_signature_method=" + WebUtility.UrlEncode("HMAC-SHA1"))
    data.Append("&oauth_timestamp=" + WebUtility.UrlEncode("1318467427"))
    data.Append("&oauth_version=" + WebUtility.UrlEncode("1.0"))
    ' Create a byte array of the data we want to send  
    byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())

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

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

    Try
        ' Get response  
        response = DirectCast(request.GetResponse(), HttpWebResponse)
        ' Get the response stream into a reader  
        reader = New StreamReader(response.GetResponseStream())
        ' Console application output  
        Console.WriteLine(reader.ReadToEnd())
    Catch ex As Exception
        ' Update form textbox in threadsafe manner that posting to API failed
        'SetTextBoxText_ThreadSafe(Me.logTxtBox, "Error contacting server to update client data." + vbCrLf)
        MsgBox(ex.ToString())
    Finally
        If Not response Is Nothing Then response.Close()
    End Try
End Sub

End Class
`

Any help would be great, thanks!