(401) Unauthorized

Posted on 2014-08-23
Last Modified: 2014-08-25
Hi experts,

I am following the below link  and trying to post to my twitter account but receiving the below error, can you please advise what I am missing.

Error that I am receiving:

Server Error in '/' Application.
The remote server returned an error: (401) Unauthorized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:
Line 34:                 string pin = Request["oauth_verifier"].ToString();
Line 35:
Line 36:                 var tokens = OAuthUtility.GetAccessToken(
Line 37:                     oauth_consumer_key,
Line 38:                    oauth_consumer_secret,

Source File: c:\Twitter\page4.aspx.cs    Line: 36

Stack Trace:

[WebException: The remote server returned an error: (401) Unauthorized.]
   System.Net.HttpWebRequest.GetResponse() +6592536
   Twitterizer.WebRequestBuilder.ExecuteRequest() +64
   Twitterizer.OAuthUtility.GetAccessToken(String consumerKey, String consumerSecret, String requestToken, String verifier, WebProxy proxy) +550

[TwitterizerException: The remote server returned an error: (401) Unauthorized.]
   Twitterizer.OAuthUtility.GetAccessToken(String consumerKey, String consumerSecret, String requestToken, String verifier, WebProxy proxy) +706
   Twitterizer.OAuthUtility.GetAccessToken(String consumerKey, String consumerSecret, String requestToken, String verifier) +50
   L1Twitter.Challenge4.btnDone_Click(Object sender, EventArgs e) in c:\Twitter\page4.aspx.cs:36
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9750842
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +196
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Twitterizer;

namespace Twitter
    public partial class page4 : System.Web.UI.Page

        protected void Click(object sender, EventArgs e)
            var oauth_consumer_key = "4K0zIxoVlQb4ach4gAZT";
            var oauth_consumer_secret = "tQgZARLhyLjT20EdkpRt30nn0q5skQiT9qINAMU0x";

            if (Request["Oauth_token"] == null)
                OAuthTokenResponse reqToken = OAuthUtility.GetRequestToken(

                string requestToken = Request["oauth_token"].ToString();
                string pin = Request["oauth_verifier"].ToString();

                var tokens = OAuthUtility.GetAccessToken(

                OAuthTokens accesstoken = new OAuthTokens()
                    AccessToken = tokens.Token,
                    AccessTokenSecret = tokens.TokenSecret,
                    ConsumerKey = oauth_consumer_key,
                    ConsumerSecret = oauth_consumer_secret

                TwitterResponse<TwitterStatus> response = TwitterStatus.Update(
                    "Testing!! It works");

                if (response.Result ==  RequestResult.Success)
                    Response.Write("we did it!");
                    Response.Write("Its all Working");

Open in new window

Question by:ipsec600
    LVL 58

    Accepted Solution

    The code it is based on is defunct and will never work anymore

    Author Comment

    Thank you so much Gary, could you please advise me any available link that I can follow.
    LVL 58

    Assisted Solution

    This one is still current

    Else there is a few more here

    Author Closing Comment

    Thank you so much Gary for your support.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now