谷歌API天气403错误 [英] Google Weather API 403 Error

查看:156
本文介绍了谷歌API天气403错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定从谷歌的天气API提取信息 - 将code,我用下面正常工作

I decided to pull information from Google's Weather API - The code I'm using below works fine.

            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");
        }
     }

正如我所说的,我能够从XML文件中提取所需的数据并显示它,但是如果页面刷新或当前会话仍处于活动状态,我收到以下错误:

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:

引发WebException是由用户code未处理 - 远程服务器返回
  错误:403禁止异常

WebException was unhandled by user code - The remote server returned an error: 403 Forbidden Exception.

我不知道这是否是具有某种访问限制的把访问该特定的XML文件?

I'm wondering whether this could be to do with some kind of access limitation put on access to that particular XML file?

如下所述,这绝不是最好的做法,但我已经包括抓我现在使用的除外。我运行的Page_Load这个code,所以我只是做了后回页面。因为我还没有发现任何问题。性能明智我并不过于担心 - 我还没有发现在加载时间的增加,这种解决方案是暂时由于这一事实,这是所有用于测试目的。我仍然在使用雅虎的天气API的过程。

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.

        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 &&
                exp.Response != null)
            {
                var webres = (HttpWebResponse)exp.Response;
                if (webres.StatusCode == HttpStatusCode.Forbidden)
                {
                    Response.Redirect(ithwidgedev.aspx);
                }

            }
        }

谷歌的文章说明API错误处理

谷歌API处理错误

http://stackoverflow.com/a/12011819/1302173 (捕捉403和回忆)

http://stackoverflow.com/a/12011819/1302173 (Catch 403 and recall)

http://stackoverflow.com/a/11883388/1302173 (错误处理和一般API谷歌资讯)

http://stackoverflow.com/a/11883388/1302173 (Error Handling and General Google API info)

http://stackoverflow.com/a/12000806/1302173 (响应处理/ JSON缓存 - 未来计划)

http://stackoverflow.com/a/12000806/1302173 (Response Handling/json caching - Future plans)

我发现这个伟大的开源替代品最近

I found this great open source alternative recently

OpenWeatherMap - 免费气象资料和预报API

推荐答案

这是关系到服务的变化/停运。请参阅: http://status-dashboard.com/32226/47728

This is related to a change / outage of the service. See: http://status-dashboard.com/32226/47728

我一直使用谷歌的API天气超过一年,使宝利通的手机收到一个天气网页养活一个电话服务器。它已经无故障运行了一年多。截至8月7日2012年曾多次间歇性的403错误。

I have been using Google's Weather API for over a year to feed a phone server so that the PolyCom phones receive a weather page. It has run error free for over a year. As of August 7th 2012 there have been frequent intermittent 403 errors.

我让服务每小时一次的一击(如一直如此),所以我不认为请求频率是问题。更有可能的是403的间歇性有关,部分转出配置更改或在谷歌CDN的变化。

I make a hit of the service once per hour (As has always been the case) so I don't think frequency of request is the issue. More likely the intermittent nature of the 403 is related to the partial roll-out of a configuration change or a CDN change at Google.

在谷歌API天气是不是一个真正的发布的API。这显然​​是设计用于在iGoogle上这样的支持程度是不确定的内部服务。我昨天啾啾googleapis并没有收到任何答复。

The Google Weather API isn't really a published API. It was an internal service apparently designed for use on iGoogle so the level of support is uncertain. I tweeted googleapis yesterday and received no response.

有可能更好切换到促进的天气的API,如:
     WUnderground天气
    雅虎天气的。

It may be better to switch to a promoted weather API such as: WUnderground Weather or Yahoo Weather.

我添加了以下',除非定义的错误处理的perl code自己昨天以应付这一点,但如果问题仍然存在,我会切换到更全面支持服务:

I have added the following 'unless defined' error handling perl code myself yesterday to cope with this but if the problem persists I will switch to a more fully supported service:

my $url = "http://www.google.com/ig/api?weather=" . $ZipCode ;

my $tpp = XML::TreePP->new();
my $tree = $tpp->parsehttp( GET => $url );

my $city = $tree->{xml_api_reply}->{weather}->{forecast_information}->{city}->{"-data"};

unless (defined($city)) {
    print "The weather service is currently unavailable. \n";
    open (MYFILE, '>/home/swarmp/public_html/status/polyweather.xhtml');
    print MYFILE qq(<?xml version="1.0" encoding="utf-8"?>\n);
    print MYFILE qq(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">\n);
    print MYFILE qq(<html xmlns="http://www.w3.org/1999/xhtml">\n);
    print MYFILE qq(<head><title>Weather is Unavailable!</title></head>\n);
    print MYFILE qq(<body>\n);
    print MYFILE qq(<p>\n);
    print MYFILE qq(The weather service is currently unavailable from the data vendor.\n);
    print MYFILE qq(</p>\n);
    print MYFILE qq(</body>\n);
    print MYFILE qq(</html>\n);
    close MYFILE;
    exit(0);
}...

这篇关于谷歌API天气403错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆