Skip to content

Instantly share code, notes, and snippets.

@haseebtirur
Forked from VerizonMediaOwner/weather_ydn_sample.java
Last active March 15, 2019 07:59
Show Gist options
  • Save haseebtirur/b79f71acb2d1549b73ca4034e87c75aa to your computer and use it in GitHub Desktop.
Save haseebtirur/b79f71acb2d1549b73ca4034e87c75aa to your computer and use it in GitHub Desktop.
Yahoo Weather API C# Example
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);
}
}
@abdulkhaliqsixlogics
Copy link

Anyone, Please share Yahoo weather Sample project.

@haseebtirur
Copy link
Author

haseebtirur commented Mar 7, 2019

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.

@abdulkhaliqsixlogics
Copy link

abdulkhaliqsixlogics commented Mar 15, 2019

Here is the complete example of Yahoo weather by location Name.

Yahoo Weather Example Link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment