ddo / oauth-1.0a

OAuth 1.0a Request Authorization for Node and Browser
MIT License
325 stars 116 forks source link

oauth_signature does not match expected value in C# while get acess token in thmblr #85

Closed kamalkumar1 closed 5 years ago

kamalkumar1 commented 5 years ago

`using (HttpClient client = new HttpClient()) { client.BaseAddress = new Uri("https://www.tumblr.com/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            TimeSpan timestamp = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            string nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));

            Dictionary<string, string> oauth = new Dictionary<string, string>
            {
                ["oauth_consumer_key"] = consumerkey,
                ["oauth_token"] = split[0],
                ["oauth_signature_method"] = "HMAC-SHA1",
                ["oauth_timestamp"] = Convert.ToInt64(timestamp.TotalSeconds).ToString(),
                ["oauth_nonce"] = new string(nonce.Where(c => char.IsLetter(c) || char.IsDigit(c)).ToArray()),
                ["oauth_version"] = "1.0",
                ["oauth_verifier"]=split[1]
            };

            string[] parameterCollectionValues = oauth.Select(parameter =>
                    Uri.EscapeDataString(parameter.Key) + "=" +
                    Uri.EscapeDataString(parameter.Value))
                .OrderBy(kv => kv)
                .ToArray();
            string parameterCollection = string.Join("&", parameterCollectionValues);

            string baseString = "POST";
            baseString += "&";
            baseString += Uri.EscapeDataString(client.BaseAddress + string.Format("oauth/access_token?oauth_verifier={0}", split[1]));
            baseString += "&";
            baseString += Uri.EscapeDataString(parameterCollection);

            string signingKey = Uri.EscapeUriString(Uri.EscapeDataString(secretkey) + "&" + Uri.EscapeDataString(split[2]));

            HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));
           // oauth["oauth_signature"] = System.Net.WebUtility.UrlEncode(Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString))));
            oauth["oauth_signature"] = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString)));
            var codes = Uri.EscapeDataString(Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString))));
            string headerString = "OAuth ";
            string[] headerStringValues = oauth.Select(parameter =>
                    Uri.EscapeDataString(parameter.Key) + "=" + "\"" + 
                    Uri.EscapeDataString(parameter.Value) + "\"")
                .ToArray();
            headerString += string.Join(", ", headerStringValues);

           client.DefaultRequestHeaders.Add("Authorization", headerString);
         //client.DefaultRequestHeaders.Add("Authorization", "OAuth");

        //var data = string.Format("oauth/access_token?oauth_consumer_key={0}&oauth_token={1}&oauth_signature_method={2}&oauth_timestamp={3}&oauth_nonce={4}&oauth_version={5}&oauth_verifier={6}&oauth_signature={7}", 
                //Uri.EscapeDataString(consumerkey),Uri.EscapeDataString(split[0]),"HMAC-SHA1",Uri.EscapeDataString(Convert.ToInt64(timestamp.TotalSeconds).ToString()),
                //Uri.EscapeDataString(new string(nonce.Where(c => char.IsLetter(c) || char.IsDigit(c)).ToArray())),"1.0",Uri.EscapeDataString(split[1]),codes);

            HttpResponseMessage response = await client.GetAsync(string.Format("oauth/access_token?oauth_verifier={0}",split[1]));
           // HttpResponseMessage response = await client.GetAsync(data);
            string json = await response.Content.ReadAsStringAsync();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                return responseString;
            }
            else
            {
                return null;
            }

        }`
ddo commented 5 years ago

i really don't understand why there is C# here