401 error. Not authorized


#1

Hi, i´m new as a twitter developer. I hope someone can help me. I´m trying to perform a HttpWebRequest who send me tredns in json. I´m using vb.net and i always obtain the same result “401 error. Not authorized”.
I have my ConsumerKey, ConsumerSecret, token, tokenSecret in the app.config file. I´m obtained this values when I registred as a twitter developer.
This is my code:

Private Sub btnLibreria_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLibreria.Click
        Dim miOAuth As New OAuth.OAuthBase 'I dowloaded this file from http://oauth.googlecode.com/svn/code/vbnet/oAuth.vb

        Dim nonce As String = miOAuth.GenerateNonce() 'generate a nonce string
        Dim timeStamp As String = miOAuth.GenerateTimeStamp() 'generate time in seconds

        Dim apiurl As New Uri("https://api.twitter.com/1.1/trends/place.json?id=1")
        'obtain the signature
        Dim firma As String = miOAuth.GenerateSignature(apiurl, _
                                                                System.Configuration.ConfigurationSettings.AppSettings("ConsumerKey"), _
                                                                System.Configuration.ConfigurationSettings.AppSettings("ConsumerSecret"), _
                                                                System.Configuration.ConfigurationSettings.AppSettings("token"), _
                                                                System.Configuration.ConfigurationSettings.AppSettings("tokenSecret"), _
                                                                "GET", _
                                                                timeStamp, _
                                                                nonce, _
                                                                "", "")


        'configuro propiedades del objeto request
        request = CType(WebRequest.Create("https://api.twitter.com/1.1/trends/place.json?id=1"), HttpWebRequest)

        request.KeepAlive = True
        request.Headers.Set("X-HostCommonName", "api.twitter.com")
        request.ContentType = "application/x-www-form-urlencoded"
        request.Accept = "*/*"
        request.UserAgent = "OAuth gem v0.4.4"
        request.Method = "GET"


        request.Headers.Add("Authorization: OAuth oauth_consumer_key=""" & rfc3986_encode(System.Configuration.ConfigurationSettings.AppSettings("ConsumerKey")) & _
                            """, oauth_nonce=""" & nonce & _
                            """, oauth_signature=""" & rfc3986_encode(firma) & "" & _
                            """, oauth_signature_method=""HMAC-SHA1"", oauth_timestamp=""" & timeStamp & _
                            """, oauth_token=""" & rfc3986_encode(System.Configuration.ConfigurationSettings.AppSettings("token")) & _
                            """, oauth_version=""1.0""")

        request.ContentLength = 0
        request.Host = "api.twitter.com"

        'txtRespuesta.Text = request.Headers.ToString
        'Exit Sub
        Try
            resp = CType(request.GetResponse, HttpWebResponse)
            txtRespuesta.Text = resp.StatusCode & " el código 200 es conexión correcta."
        Catch ex As Exception
            txtRespuesta.Text = ex.Message
        End Try
    End Sub

    Private Function rfc3986_encode(ByVal stringAcode As String) As String
        Dim result As String = stringAcode
        result = result.Replace("%7E", "~")
        result = result.Replace("=", "%3D")
        result = result.Replace("+", "%2B")
        result = result.Replace(":", "%3A")
        result = result.Replace("/", "%2F")
        Return result
    End Function

I don´t know what I must to change in order to do make it work.
Thank you for your time!.