Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle Weather API 403 Error
    text
    copied!<p>I decided to pull information from Google's Weather API - The code I'm using below works fine.</p> <pre><code> XmlDocument widge = new XmlDocument(); widge.Load("https://www.google.com/ig/api?weather=Brisbane/dET7zIp38kGFSFJeOpWUZS3-"); var weathlist = widge.GetElementsByTagName("current_conditions"); foreach (XmlNode node in weathlist) { City.Text = ("Brisbane"); CurCond.Text = (node.SelectSingleNode("condition").Attributes["data"].Value); Wimage.ImageUrl = ("http://www.google.com/" + node.SelectSingleNode("icon").Attributes["data"].Value); Temp.Text = (node.SelectSingleNode("temp_c").Attributes["data"].Value + "°C"); } } </code></pre> <p>As I said, I am able to pull the required data from the XML file and display it, however if the page is refreshed or a current session is still active, I receive the following error:</p> <blockquote> <p>WebException was unhandled by user code - The remote server returned an error: 403 Forbidden Exception.</p> </blockquote> <p>I'm wondering whether this could be to do with some kind of access limitation put on access to that particular XML file?</p> <h1>Further research and adaptation of suggestions</h1> <p>As stated below, this is by no means best practice, but I've included the catch I now use for the exception. I run this code on Page_Load so I just do a post-back to the page. I haven't noticed any problems since. Performance wise I'm not overly concerned - I haven't noticed any increase in load time and this solution is temporary due to the fact this is all for testing purposes. I'm still in the process of using Yahoo's Weather API.</p> <pre><code> try { XmlDocument widge = new XmlDocument(); widge.Load("https://www.google.com/ig/api?weather=Brisbane/dET7zIp38kGFSFJeOpWUZS3-"); var list2 = widge.GetElementsByTagName("current_conditions"); foreach (XmlNode node in list2) { City.Text = ("Brisbane"); CurCond.Text = (node.SelectSingleNode("condition").Attributes["data"].Value); Wimage.ImageUrl = ("http://www.google.com/" + node.SelectSingleNode("icon").Attributes["data"].Value); Temp.Text = (node.SelectSingleNode("temp_c").Attributes["data"].Value + "°C"); } } catch (WebException exp) { if (exp.Status == WebExceptionStatus.ProtocolError &amp;&amp; exp.Response != null) { var webres = (HttpWebResponse)exp.Response; if (webres.StatusCode == HttpStatusCode.Forbidden) { Response.Redirect(ithwidgedev.aspx); } } } </code></pre> <h1>Google article illustrating API error handling</h1> <p><a href="https://developers.google.com/drive/handle-errors" rel="nofollow noreferrer">Google API Handle Errors</a></p> <h1>Thanks to:</h1> <p><a href="https://stackoverflow.com/a/12011819/1302173">https://stackoverflow.com/a/12011819/1302173</a> (Catch 403 and recall)</p> <p><a href="https://stackoverflow.com/a/11883388/1302173">https://stackoverflow.com/a/11883388/1302173</a> (Error Handling and General Google API info)</p> <p><a href="https://stackoverflow.com/a/12000806/1302173">https://stackoverflow.com/a/12000806/1302173</a> (Response Handling/json caching - Future plans)</p> <h1>Alternative</h1> <p>I found this great open source alternative recently</p> <p><a href="http://openweathermap.org/API#weather" rel="nofollow noreferrer">OpenWeatherMap - Free weather data and forecast API</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload