Help! 400 bad request. Change something and 401 unauthorized

dotnet

#1
Imports System.Globalization
Imports System.Security.Cryptography
Imports System.Net
Imports System.IO

Public Class Giris
    Inherits System.Web.UI.Page

    Dim twitterResourceURL As String = "https://api.twitter.com/oauth/request_token"
    Dim twitterOAuthNonce As String = ""
    Dim twitterOAuthCallback As String = ""
    Dim twitterOAuthSignatureMethod As String = "HMAC-SHA1"
    Dim twitterOAuthTimestamp As String = ""
    Dim twitterOAuthConsumerKey As String = "xxxx"
    Dim twitterOAuthSignature As String = ""
    Dim twitterOAuthVersion As String = "1.0"

    Dim twitterOAuthConsumerSecret As String = "xxxx"
    Dim twitterOAuthToken As String = "xxxx"
    Dim twitterOAuthTokenSecret As String = "xxxx"

    Dim compositeKey As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'CultureInfo => Imports System.Globalization
        twitterOAuthNonce = Convert.ToBase64String(New ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture)))
        Dim timeSpan = DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)
        twitterOAuthTimestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString(CultureInfo.InvariantCulture)
        twitterOAuthSignature = create_auth_Signature_Task(twitterOAuthNonce, twitterOAuthCallback, twitterOAuthSignatureMethod, twitterOAuthTimestamp, twitterOAuthConsumerKey, twitterOAuthVersion, twitterResourceURL)

        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(twitterResourceURL), HttpWebRequest)
        request.Method = "POST"
        request.Host = "api.twitter.com"

        request.Headers.Add(HttpRequestHeader.Authorization, "OAuth oauth_callback=""" + Uri.EscapeDataString(twitterOAuthCallback) + """" + ",oauth_consumer_key=""" + twitterOAuthConsumerKey + """,oauth_signature=""" + Uri.EscapeDataString(twitterOAuthSignature) + """,oauth_signature_method=""HMAC-SHA1"",oauth_timestamp=""" + twitterOAuthTimestamp + """,oauth_version = ""1.0""")

        Dim response As WebResponse
        Response = request.GetResponse()


    End Sub

    Private Function create_auth_Signature_Task(oauth_nonce As String, oauth_callback As String, oauth_signature_method As String, oauth_timestamp As String, oauth_consumer_key As String, oauth_version As String, req_url As String) As String
        Dim oauth_nonce_enc As String = Uri.EscapeDataString(oauth_nonce)
        Dim oauth_callback_enc As String = Uri.EscapeDataString(oauth_callback)
        Dim oauth_signature_method_enc As String = Uri.EscapeDataString(oauth_signature_method)
        Dim oauth_timestamp_enc As String = Uri.EscapeDataString(oauth_timestamp)
        Dim oauth_consumer_key_enc As String = Uri.EscapeDataString(oauth_consumer_key)
        Dim oauth_version_enc As String = Uri.EscapeDataString(oauth_version)
        Dim req_url_enc As String = Uri.EscapeDataString(req_url)
        Dim signature_base As String = (Convert.ToString("POST&") & req_url_enc) + "&" + Uri.EscapeDataString(Convert.ToString((Convert.ToString((Convert.ToString((Convert.ToString((Convert.ToString("oauth_consumer_key=") & oauth_consumer_key_enc) + "&oauth_nonce=") & oauth_nonce) + "&oauth_signature_method=") & oauth_signature_method_enc) + "&oauth_timestamp=") & oauth_timestamp_enc) + "&oauth_version=") & oauth_version_enc)

        Dim signingKey As String = twitterOAuthConsumerSecret + "&" + twitterOAuthTokenSecret

        Return ShaHash(signature_base, signingKey)
    End Function



    Private Function ShaHash(value As String, key As String) As String
        Using hmac = New HMACSHA1(Encoding.UTF32.GetBytes(key))
            Return ByteToString(hmac.ComputeHash(Encoding.UTF32.GetBytes(value)))
        End Using
    End Function

    Private Shared Function ByteToString(data As IEnumerable(Of Byte)) As String
        Return String.Concat(data.[Select](Function(b) b.ToString("x2")))
    End Function

End Class