Last active
March 6, 2022 20:39
-
-
Save VerizonMediaOwner/eea82f47f3d688e466feac494696ca9e to your computer and use it in GitHub Desktop.
Yahoo Weather API C# Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Yahoo Weather API C# Sample Code | |
// Code sample offered under the terms of the CC0 Public Domain designation. See https://creativecommons.org/publicdomain/zero/1.0/legalcode/ for terms. | |
// Author: Eugene Plotnikov | |
using System; | |
using System.Net; | |
using System.Security.Cryptography; | |
using System.Text; | |
class YWSample { | |
const string cURL = "https://weather-ydn-yql.media.yahoo.com/forecastrss"; | |
const string cAppID = "test-app-id"; | |
const string cConsumerKey = "your-consumer-key"; | |
const string cConsumerSecret = "your-consumer-secret"; | |
const string cOAuthVersion = "1.0"; | |
const string cOAuthSignMethod = "HMAC-SHA1"; | |
const string cWeatherID = "woeid=727232"; // Amsterdam, The Netherlands | |
const string cUnitID = "u=c"; // Metric units | |
const string cFormat = "xml"; | |
static string _get_timestamp () { | |
TimeSpan lTS = DateTime.UtcNow - new DateTime ( 1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc ); | |
return Convert.ToInt64 ( lTS.TotalSeconds ).ToString (); | |
} // end _get_timestamp | |
static string _get_nonce () { | |
return Convert.ToBase64String ( | |
new ASCIIEncoding ().GetBytes ( | |
DateTime.Now.Ticks.ToString () | |
) | |
); | |
} // end _get_nonce | |
// NOTE: whenever the value of a parameter is changed, say cUnitID "u=c" => "location=sunnyvale,ca" | |
// The order in lSign needs to be updated, i.e. re-sort lSign | |
// Please don't simply change value of any parameter without re-sorting. | |
static string _get_auth () { | |
string retVal; | |
string lNonce = _get_nonce (); | |
string lTimes = _get_timestamp (); | |
string lCKey = string.Concat ( cConsumerSecret, "&" ); | |
string lSign = string.Format ( // note the sort order !!! | |
"format={0}&" + | |
"oauth_consumer_key={1}&" + | |
"oauth_nonce={2}&" + | |
"oauth_signature_method={3}&" + | |
"oauth_timestamp={4}&" + | |
"oauth_version={5}&" + | |
"{6}&{7}", | |
cFormat, | |
cConsumerKey, | |
lNonce, | |
cOAuthSignMethod, | |
lTimes, | |
cOAuthVersion, | |
cUnitID, | |
cWeatherID | |
); | |
lSign = string.Concat ( | |
"GET&", Uri.EscapeDataString ( cURL ), "&", Uri.EscapeDataString ( lSign ) | |
); | |
using ( var lHasher = new HMACSHA1 ( Encoding.ASCII.GetBytes( lCKey ) ) ) { | |
lSign = Convert.ToBase64String ( | |
lHasher.ComputeHash ( Encoding.ASCII.GetBytes ( lSign ) ) | |
); | |
} // end using | |
return "OAuth " + | |
"oauth_consumer_key=\"" + cConsumerKey + "\", " + | |
"oauth_nonce=\"" + lNonce + "\", " + | |
"oauth_timestamp=\"" + lTimes + "\", " + | |
"oauth_signature_method=\"" + cOAuthSignMethod + "\", " + | |
"oauth_signature=\"" + lSign + "\", " + | |
"oauth_version=\"" + cOAuthVersion + "\""; | |
} // end _get_auth | |
public static void Main ( string[] args ) { | |
const string lURL = cURL + "?" + cWeatherID + "&" + cUnitID +"&format=" + cFormat; | |
var lClt = new WebClient (); | |
lClt.Headers.Set ( "Content-Type", "application/" + cFormat ); | |
lClt.Headers.Add ( "X-Yahoo-App-Id", cAppID ); | |
lClt.Headers.Add ( "Authorization", _get_auth () ); | |
Console.WriteLine( "Downloading Yahoo weather report . . ." ); | |
byte[] lDataBuffer = lClt.DownloadData ( lURL ); | |
string lOut = Encoding.ASCII.GetString ( lDataBuffer ); | |
Console.WriteLine ( lOut ); | |
Console.Write ( "Press any key to continue . . . " ); | |
Console.ReadKey ( true ); | |
} // end Main | |
} // end YWSample |
retVal is not used here
So we do not need it.
You may update from my fork
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to download weather data by ZIP and LatLong?