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
instead of
HttpWebRequest
, you could useHttpClient
which supportasync
request.Try to use this code block
P.S Keep in mind that instead of creating a new instance of
HttpClient
for each execution you should share a single instance ofHttpClient
for the entire lifetime of the application otherwise you may encounter this errorMore about this here
Just use oauth-dotnetcore https://github.com/rhargreaves/oauth-dotnetcore. It would help with creating your Oauth 1.0a authorization headers.
Then you can get the authourization header like this:
then use whatever client to make the http request.