-
-
Save haseebtirur/b79f71acb2d1549b73ca4034e87c75aa to your computer and use it in GitHub Desktop.
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Security.Cryptography; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Web; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string appId = "8eqyZg6s"; | |
const string consumerKey = "dj0yJmk9dkw3YWVLTEdXUmNRJnM9Y29uc3VtZXJzZWNyZXQmc3Y9MCZ4PTM3"; | |
const string consumerSecret = "fd427dd3ec3da0046fbd78ac1444f1a660259c6d"; | |
const string url = "https://weather-ydn-yql.media.yahoo.com/forecastrss"; | |
long timeStamp = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; | |
byte[] nonce = new byte[32]; | |
Random rand = new Random(); | |
rand.NextBytes(nonce); | |
string oauthNonce = new string(Convert.ToBase64String(nonce).ToArray()); | |
oauthNonce = Regex.Replace(oauthNonce, @"[^0-9a-zA-Z]+", ""); | |
List<string> parameters = new List<string> | |
{ | |
"oauth_consumer_key=" + consumerKey, | |
"oauth_nonce=" + oauthNonce, | |
"oauth_signature_method=HMAC-SHA1", | |
"oauth_timestamp=" + timeStamp, | |
"oauth_version=1.0", | |
// Make sure value is encoded | |
"location=" + HttpUtility.UrlEncode("sunnyvale,ca", Encoding.UTF8), | |
"format=json" | |
}; | |
parameters.Sort(); | |
StringBuilder parametersList = new StringBuilder(); | |
for (int i = 0; i < parameters.Count; i++) | |
{ | |
parametersList.Append(((i > 0) ? "&" : "") + parameters[i]); | |
} | |
string signatureString = "GET&" + | |
HttpUtility.UrlEncode(url, Encoding.UTF8) + "&" + | |
HttpUtility.UrlEncode(parametersList.ToString(), Encoding.UTF8); | |
string signature = null; | |
try | |
{ | |
byte[] signingKey = Encoding.UTF8.GetBytes(consumerSecret); | |
HMACSHA1 hmac = new HMACSHA1(signingKey); | |
byte[] rawHmac = hmac.ComputeHash(Encoding.UTF8.GetBytes(signatureString)); | |
signature = HttpUtility.UrlEncode(Convert.ToBase64String(rawHmac), Encoding.UTF8); | |
} | |
catch (Exception) | |
{ | |
System.Diagnostics.Debug.WriteLine("Unable to append signature"); | |
Environment.Exit(0); | |
} | |
string authorizationLine = "OAuth " + | |
"oauth_consumer_key=\"" + consumerKey + "\", " + | |
"oauth_nonce=\"" + oauthNonce + "\", " + | |
"oauth_timestamp=\"" + timeStamp + "\", " + | |
"oauth_signature_method=\"HMAC-SHA1\", " + | |
"oauth_signature=\"" + signature + "\", " + | |
"oauth_version=\"1.0\""; | |
var request = (HttpWebRequest)WebRequest.Create(url + "?location=sunnyvale,ca&format=json"); | |
request.Headers.Add("Authorization", authorizationLine); | |
request.Headers.Add("Yahoo-App-Id", appId); | |
request.ContentType = "application/json"; | |
Console.WriteLine(request); | |
Console.ReadLine(); | |
var response = (HttpWebResponse)request.GetResponse(); | |
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); | |
Console.WriteLine(responseString); | |
} | |
} |
unfortunatly doesnot work for me!
Neither for me. It returns 401: Unauthorised
It Work...
Only update line 53
from : byte[] signingKey = Encoding.UTF8.GetBytes(consumerSecret);
to : byte[] signingKey = Encoding.UTF8.GetBytes(consumerSecret + "&");
Thankful. :)
consumerSecret + "&");
This is not working for me, oh! I have found interesting thing. HttpUtility encodes the string in lower case, and yahoo API can not understand this encoding. And Java example (on docs of api) works correctly, because it decodes in upper case. I am not sure in this, but I have read this on some Q&A web-suite. I can not find it, sorry.
unfortunately it doesn't work for me too!
Anyone, Please share Yahoo weather Sample project.
Hi all,
I am archiving this code. Yahoo has published C# code in their own docs. Please use it.
https://developer.yahoo.com/weather/documentation.html#oauth-csharp
Thanks.
Here is the complete example of Yahoo weather by location Name.
unfortunatly doesnot work for me!