Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI
I am getting an error "The remote server returned an error: (401) Unauthorized."

i have seen in so many sites but none of them solved the problem.

I am trying to fix this problem past 2 days but iam not able to do so. Please help me with some sample codes.

This is the code am using now,

VB
Sub TwitIt(ByVal strUser As String, ByVal strPass As String, ByVal strMessage As String)
        'this subroutine requires your ASP.NET page to have a label control with an ID of lblStatus
        'create post variable for tweet
        Dim strTweet As String = "status=" & Server.HtmlEncode(strMessage)
        'convert post variable to byte array for transmission purposes
        Dim bRequest As Byte() = System.Text.Encoding.ASCII.GetBytes(strTweet)
        Try
            'create HttpWebRequest to status update API resource
            Dim objRequest As HttpWebRequest = WebRequest.Create("http://twitter.com/statuses/update.xml")
            'pass basic authentication credentials
            objRequest.Credentials = New NetworkCredential(strUser, strPass)
            'set method to post and pass request as a form
            objRequest.Method = "POST"
            objRequest.ContentType = "application/x-www-form-urlencoded"
            'tell the server it will not receive a 100 Continue HTTP response
            objRequest.ServicePoint.Expect100Continue = False
            'set content length of request
            objRequest.ContentLength = bRequest.Length
            'capture the stream (content) of the request
            Dim objStream As Stream = objRequest.GetRequestStream()
            'put the bytes into request
            objStream.Write(bRequest, 0, bRequest.Length)
            'close the stream to complete the request
            objStream.Close()
            'uncomment line below to report success
            'lblStatus.Text = "Tweet sent!"
            'You can also capture the XML response Twitter sends back
            'uncomment lines below to capture responses
          Dim objResponse As WebResponse = objRequest.GetResponse()
            Dim objReader As New StreamReader(objResponse.GetResponseStream())
            lblStatus.Text = objReader.ReadToEnd()
        Catch ex As Exception
            'uncomment line below to report ASP.NET errors
            lblStatus.Text = ex.Message
        End Try
    End Sub



This is the line which causes that problem " Dim objResponse As WebResponse = objRequest.GetResponse()
"


Any Help would be appreciable

Thanks
Govind
Posted
Comments
Manas Bhardwaj 13-Jul-11 7:43am    
Not really sure, but I thought twitter already moved to oauth?
Govind212 13-Jul-11 7:57am    
thanks, But i tried that also ...but same error comes,
Following is the code i used

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
' add these to web.config or your preferred location
Dim consumerKey = ConfigurationManager.AppSettings("TwitConsumerKey")
Dim consumerSecret = ConfigurationManager.AppSettings("TwitConsumerSecret")

'If User is not valid user
If Request.QueryString("oauth_token") Is Nothing Then
'Step 1: Get Request Token
Dim RequestToken As OAuthTokenResponse = OAuthUtility.GetRequestToken(consumerKey, consumerSecret, Request.Url.AbsoluteUri)

'Step 2: Redirect User to Requested Token
Response.Redirect("http://twitter.com/oauth/authorize?oauth_token=" + RequestToken.Token)
Else
'For Valid User
Dim Oauth_Token As String = Request.QueryString("oauth_token").ToString()
Dim accessToken = OAuthUtility.GetAccessToken(consumerKey, consumerSecret, Oauth_Token, "")

lblStatus.Text = "Hello " & Convert.ToString(accessToken.ScreenName) & ", Welcome to Go4Sharepoint.com Twitter App"

lblStatus.Text += "<br/> Token: " & Convert.ToString(accessToken.Token)
lblStatus.Text += "<br/> TokenSecret: " & Convert.ToString(accessToken.TokenSecret)
lblStatus.Text += "<br/> UserId: " & Convert.ToString(accessToken.UserId)
End If
End Sub
Plz Help me out

1 solution

Isn't the error message self-describing? You simply need to authorize in the same session. Go to the authorization page and use "View page source" to see what parameters of authorizations are expected, what's the protocol (could be HTTPS instead of HTTP) and HttpWebRequest method (should also be "POST"). Create an instance of HttpWebRequest and use it to post authorization data the same way you already use. When you are authorized, try to post you message the way you already do.

(Just in case: hope you understand, that if you passed authorization using a Web browser, it cannot help. The sessions are identified on the per-browser basis. Your application should play the role of browser from the very beginning.)

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900