c# - Yahoo Weather API using Oauth -


i stuck on implementing yahoo weather private api call. code snippet, whenever call using valid clientid & secret returns 401 (unauthorized).

var outhwc = new webclient(); outhwc.credentials = new networkcredential(clientid, clientsecret); outhwc.headers.add(httprequestheader.accept, "application/json"); var outhresponse = outhwc.downloaddata("https://query.yahooapis.com/v1/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3d%22nome%2c%20ak%22)&format=json"); 

it throws exception. try pass username , password in networkcredentials , try pass clientid , secret in header cannot find successful call.

so i've been stuck same issue here. implemented following code, based on oauth c# simple class found @ http://oauth.googlecode.com/svn/code/csharp/oauthbase.cs

   public void loadweather() {         string urldes, params = "";         string signature, baseurl, ckey, csecret = "";          oauthbase oauth = new oauthbase();         baseurl = "http://weather.yahooapis.com/forecastrss?w=" + textbox1.text + "&u=f";         ckey = "your api key";         csecret = "your api secret";          signature = oauth.generatesignature(new uri(baseurl), ckey, csecret, "", "", "get", oauth.generatetimestamp(), oauth.generatenonce(), out urldes, out params);          webclient wc = new webclient();         wc.downloadstringcompleted += httpscompleted;         wc.downloadstringasync(new uri(string.format("{0}?{1}&oauth_signature={2}", urldes, params, signature)));     }      private void httpscompleted(object sender, downloadstringcompletedeventargs e) {         if (e.error == null) {             xdocument xdoc = xdocument.parse(e.result, loadoptions.none);             xdoc.save("c:\\weather.xml");             richtextbox1.text = xdoc.firstnode.tostring();         } else {             richtextbox1.text = e.error.message;         }     } 

as can see, have city id. sample downloads xml string returned api. worked me, hope helps!


Comments