Hi, IÂŽm working with VB.NET and I want to make authorized calls to Twitterâs APIs. I obtained my access token like this https://dev.twitter.com/docs/auth/tokens-devtwittercom, because I just want to access the API from my own account. I allways obtain the same response, 401 error, not authorized. Can somebody help me?.
This is my code:
ServicePointManager.Expect100Continue = False
Dim oauth_consumer_key As String = System.Configuration.ConfigurationSettings.AppSettings("ConsumerKey")
Dim oauth_consumer_secret As String = System.Configuration.ConfigurationSettings.AppSettings("ConsumerSecret")
Dim oauth_token As String = System.Configuration.ConfigurationSettings.AppSettings("token")
Dim oauth_token_secret As String = System.Configuration.ConfigurationSettings.AppSettings("tokenSecret")
Dim oauth_version As String = "1.0"
Dim oauth_signature_method = "HMAC-SHA1"
Dim miOAuth As New OAuth.OAuthBase
Dim oauth_nonce As String = miOAuth.GenerateNonce() 'create a nonce string
Dim oauth_timestamp As String = miOAuth.GenerateTimeStamp() 'create the time in seconds
Dim apiurl As New Uri("https://api.twitter.com/1.1/trends/place.json?id=1")
'generate my signature
Dim oauth_signature As String = miOAuth.GenerateSignature(apiurl, _
oauth_consumer_key, _
oauth_consumer_secret, _
oauth_token, _
oauth_token_secret, _
âGETâ, _
oauth_timestamp, _
oauth_nonce, _
"", ââ)
'configuro propiedades del objeto request
request = CType(WebRequest.Create("https://api.twitter.com/1.1/trends/place.json?id=1"), HttpWebRequest)
Dim headerFormat = "OAuth oauth_nonce=""{0}"", oauth_signature_method=""{1}"", oauth_timestamp=""{2}"", oauth_consumer_key=""{3}"", oauth_token=""{4}"", oauth_signature=""{5}"", oauth_version=""{6}"""
Dim authHeader = String.Format(headerFormat,
Uri.EscapeDataString(oauth_nonce),
Uri.EscapeDataString(oauth_signature_method),
Uri.EscapeDataString(oauth_timestamp),
Uri.EscapeDataString(oauth_consumer_key),
Uri.EscapeDataString(oauth_token),
Uri.EscapeDataString(oauth_signature),
Uri.EscapeDataString(oauth_version)
)
request.Headers.Add("Authorization", authHeader)
request.Method = "GET"
request.ContentType = "application/x-www-form-urlencoded"
Try
resp = CType(request.GetResponse, HttpWebResponse)
txtRespuesta.Text = resp.StatusCode
Catch ex As Exception
txtRespuesta.Text = ex.Message
End Try
And this is my request header:
Authorization: OAuth oauth_nonce=â5552950â, oauth_signature_method=âHMAC-SHA1â, oauth_timestamp=â1359535898â, oauth_consumer_key=âxxxxxxxxxxxxxxxxxxxxxâ, oauth_token=âxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxâ, oauth_signature=â0uKDNswlUfs42XnUjxLuwkkmAW8%3Dâ, oauth_version="1.0"
Content-Type: application/x-www-form-urlencoded
Thanks