skip to Main Content

I’m trying to get request token from Twitter oauth/request_token API for the user based further authentication. The code return 400 bad request error. I know there could be a small glitch in the request which I am not able to catch. I have tried to match everything according to the documentation but somewhere something is missing. Any leads would be highly appreciable.

        public void get_Oauth_token()
    {
        string oauth_nonce;
        string oauth_callback;
        string oauth_signature_method;
        string oauth_timestamp;
        string oauth_consumer_key;
        string oauth_signature;
        string oauth_version;
        string req_url;
        //set values to variables
        var dt = DateTime.Now;
        var ticks = dt.Ticks;
        var seconds = ticks / TimeSpan.TicksPerSecond;
        long unixTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
        oauth_timestamp = unixTimestamp.ToString();
        oauth_callback = "http://127.0.0.1:4200/home";
        oauth_nonce = Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Substring(0, 16).ToString(); //random string
        oauth_signature_method = "HMAC-SHA1";
        oauth_consumer_key = "xxxx";
        oauth_version = "1.0";
        req_url = "https://api.twitter.com/oauth/request_token";
        oauth_signature = create_auth_Signature_Task(oauth_nonce, oauth_callback, oauth_signature_method, oauth_timestamp, oauth_consumer_key, oauth_version, req_url);




        ///action
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(req_url);
        request.Method = "POST";
        request.UserAgent = "themattharris' HTTP Client";
        request.Host = "api.twitter.com";

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


        //WebResponse response = await request.GetResponseAsync();
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        Stream str = response.GetResponseStream();
        StreamReader reader = new StreamReader(str);
        dynamic resp = reader.ReadToEndAsync();

    }

    private string create_auth_Signature_Task(string oauth_nonce, string oauth_callback,
       string oauth_signature_method, string oauth_timestamp, string oauth_consumer_key, string oauth_version, string req_url)
    {
        string oauth_nonce_enc = Uri.EscapeDataString(oauth_nonce);
        string oauth_callback_enc = Uri.EscapeDataString(oauth_callback);
        string oauth_signature_method_enc = Uri.EscapeDataString(oauth_signature_method);
        string oauth_timestamp_enc = Uri.EscapeDataString(oauth_timestamp);
        string oauth_consumer_key_enc = Uri.EscapeDataString(oauth_consumer_key);
        string oauth_version_enc = Uri.EscapeDataString(oauth_version);
        string req_url_enc = Uri.EscapeDataString(req_url);
        string secret = Uri.EscapeDataString("xxxx") + "&";
        string signature_base = "POST&" + req_url_enc + "&" + Uri.EscapeDataString("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);


        return ShaHash(signature_base, secret);
    }

    private string ShaHash(string value, string key)
    {
        using (var hmac = new HMACSHA1(Encoding.UTF32.GetBytes(key)))
        {
            return Convert.ToBase64String(hmac.ComputeHash(Encoding.ASCII.GetBytes(value)));//ByteToString(hmac.ComputeHash(Encoding.UTF32.GetBytes(value)));
        }
    }

2

Answers


  1. instead of HttpWebRequest, you could use HttpClient which support async request.
    Try to use this code block

    public async void GetToken()
    {                       
          var httpClient = new HttpClient();
          var request = new HttpRequestMessage(HttpMethod.Post, "https://api.twitter.com/oauth2/token");
    
          string oauth_consumer_key = "<consumer key>";
          string oauth_consumer_secret = "<consumer secret>";
    
          string url = "https://api.twitter.com/oauth2/token?oauth_consumer_key=" + oauth_consumer_key + "&oauth_consumer_secret=" + oauth_consumer_secret;
    
          var customerInfo = Convert.ToBase64String(new UTF8Encoding()
                              .GetBytes(oauth_consumer_key + ":" + oauth_consumer_secret));
    
          request.Headers.Add("Authorization", "Basic " + customerInfo);
          request.Content = new StringContent("grant_type=client_credentials", Encoding.UTF8,
                                                              "application/x-www-form-urlencoded");
    
          HttpResponseMessage response = await httpClient.SendAsync(request);
    
          string json = await response.Content.ReadAsStringAsync();
          var serializer = new JavaScriptSerializer();
    
          dynamic item = serializer.Deserialize<object>(json);
    
        }
    

    P.S Keep in mind that instead of creating a new instance of HttpClient for each execution you should share a single instance of HttpClient for the entire lifetime of the application otherwise you may encounter this error

    Unable to connect to the remote server
    System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.
    

    More about this here

    Login or Signup to reply.
  2. Just use oauth-dotnetcore https://github.com/rhargreaves/oauth-dotnetcore. It would help with creating your Oauth 1.0a authorization headers.

     var oauthClient = new OAuthRequest
            {
                Method = "POST",
                Type = OAuthRequestType.RequestToken,
                SignatureMethod = OAuthSignatureMethod.HmacSha1,
                ConsumerKey = consumerKey,
                ConsumerSecret = consumerSecret,
                RequestUrl = "https://api.twitter.com/oauth/request_token",
                Version = "1.0a",
                Realm = "twitter.com",
                CallbackUrl = callbackUrl
            };
    

    Then you can get the authourization header like this:

     string auth = oauthClient.GetAuthorizationHeader();
    client.DefaultRequestHeaders.Add("Authorization", auth);
    

    then use whatever client to make the http request.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search