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 |
I have test this with location name.
please check my sample code.
public class CheckWeatherDetail
{
public static string TestWeather()
{
const string url = cURL + "?location=sunnyvale" + "&" + cUnitID + "&format=" + cFormat;
using (var client = new WebClient())
{
string responseText = string.Empty; try
{
string headerString = _get_auth();
WebClient webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
webClient.Headers[HttpRequestHeader.Authorization] = headerString;
webClient.Headers.Add("X-Yahoo-App-Id", cAppID);
byte[] reponse = webClient.DownloadData(url);
string lOut = Encoding.ASCII.GetString(reponse);
}
catch (WebException exception)
{
if (exception.Response != null)
{
var responseStream = exception.Response.GetResponseStream();
if (responseStream != null)
{
using (var reader = new StreamReader(responseStream))
{
responseText = reader.ReadToEnd();
return responseText;
}
}
}
}
return string.Empty;
}
}
const string cURL = "https://weather-ydn-yql.media.yahoo.com/forecastrss";
const string cAppID = "YOurKEY";
const string cConsumerKey = "YourcConsumerKey";
const string cConsumerSecret = "YourcConsumerSecret";
const string cOAuthVersion = "1.0";
const string cOAuthSignMethod = "HMAC-SHA1";
const string cWeatherID = "sunnyvale"; // Amsterdam, The Netherlands
const string cUnitID = "u=c"; // Metric units
const string cFormat = "json";
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}&" +
"location={1}&"+
"oauth_consumer_key={2}&" +
"oauth_nonce={3}&" +
"oauth_signature_method={4}&" +
"oauth_timestamp={5}&" +
"oauth_version={6}&" +
"{7}",
cFormat,
cWeatherID,
cConsumerKey,
lNonce,
cOAuthSignMethod,
lTimes,
cOAuthVersion,
cUnitID
);
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
}
here is the complete example of Yahoo weather by location name.
https://github.com/abdulkhaliqsixlogics/Yahoo-Weather-/blob/master/YahooWeatherSample.cs
Hi, maybe someone knows how to download weather data by parameters lat and lon?
how to download weather data by ZIP and LatLong?
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
I resort the order for "location=sunnyvale,ca" but it can't works for me. can you share correct resort order for location?