Hello,
this issue has been around here a few times, yet nothing that was advised in the responses helped me solve the problem.
I have the following code in C#, (taken from http://dotnet.dzone.com/articles/getting-requesttoken-work):
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("oauth_consumer_key", ConsumerKey);
parameters.Add("oauth_signature_method", SignatureMethod);
parameters.Add("oauth_timestamp", CurrentUNIXTimestamp.Get());
parameters.Add("oauth_nonce", GetNonce());
parameters.Add("oauth_version", "1.0");
parameters = parameters.OrderBy(x => x.Key).ThenBy(x => x.Value).ToDictionary(v => v.Key, v => v.Value);
string OAuthHeader = "OAuth ", concat = "";
foreach (string k in parameters.Keys)
{
if (k == "oauth_callback")
{
concat += k + "=" + EncodeToUpper(parameters[k]) + "&";
OAuthHeader += k + "=" + "\"" + EncodeToUpper(parameters[k]) + "\", ";
}
else
{
concat += k + "=" + parameters[k] + "&";
OAuthHeader += k + "=" + "\"" + parameters[k] + "\", ";
}
}
concat = concat.Remove(concat.Length - 1);
concat = "POST&" + EncodeToUpper(RequestTokenURL) + "&" + concat;
byte[] content = Encoding.UTF8.GetBytes(concat);
HMACSHA1 hmac = new HMACSHA1(Encoding.ASCII.GetBytes(ConsumerSecret + "&"));
hmac.ComputeHash(content);
string hash = Convert.ToBase64String(hmac.Hash);
hash = hash.Replace("-", "");
OAuthHeader += "oauth_signature=\"" + EncodeToUpper(hash) + "\"";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(RequestTokenURL);
request.Method = "POST";
request.Headers["Authorization"] = OAuthHeader;
try
{
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
/*********** EXCEPTION IS THROWN ^^^ **************************/
using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
{
output = reader.ReadToEnd();
}
}
catch (WebException e)
{
output = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
}
string EncodeToUpper(string raw)
{
raw = HttpUtility.UrlEncode(raw);
return Regex.Replace(raw, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper());
}
outputting the following OAuthHeader for my consumer key:
OAuth oauth_consumer_key=“7A4vMQY0ixGqSlPYdVQmHg”, oauth_nonce=“267308179”, oauth_signature_method=“HMAC-SHA1”, oauth_timestamp=“1371727960”, oauth_version=“1.0”, oauth_signature=“dtTz%2FSiDnwx9ANQeK7dc7XiKd%2BY%3D”
What am I doing wrong?