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

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

问题描述

我决定从 Google 的 Weather API 中提取信息 - 我在下面使用的代码运行良好.

 XmlDocument widge = new XmlDocument();widge.Load("https://www.google.com/ig/api?weather=Brisbane/dET7zIp38kGFSFJeOpWUZS3-");var weathlist = widge.GetElementsByTagName("current_conditions");foreach(weathlist 中的 XmlNode 节点){City.Text = ("布里斯班");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 文件中提取所需的数据并显示它,但是如果页面刷新或当前会话仍处于活动状态,我会收到以下错误:

<块引用>

WebException 未被用户代码处理 - 远程服务器返回错误:403 禁止异常.

我想知道这是否与对该特定 XML 文件的访问设置的某种访问限制有关?

进一步研究和调整建议

如下所述,这绝不是最佳实践,但我已经包含了我现在用于异常的捕获.我在 Page_Load 上运行此代码,所以我只是回发到页面.从那以后我没有注意到任何问题.性能方面我并不过分担心 - 我没有注意到加载时间有任何增加,这个解决方案是暂时的,因为这一切都是为了测试目的.我仍在使用 Yahoo 的 Weather API.

 试试{XmlDocument widge = new XmlDocument();widge.Load("https://www.google.com/ig/api?weather=Brisbane/dET7zIp38kGFSFJeOpWUZS3-");var list2 = widge.GetElementsByTagName("current_conditions");foreach(list2 中的 XmlNode 节点){City.Text = ("布里斯班");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");}}捕获(WebException exp){if (exp.Status == WebExceptionStatus.ProtocolError &&exp.Response != null){var webres = (HttpWebResponse)exp.Response;如果(webres.StatusCode == HttpStatusCode.Forbidden){Response.Redirect(ithwidgedev.aspx);}}}

说明 API 错误处理的 Google 文章

雅虎天气.

我昨天自己添加了以下除非已定义"错误处理 perl 代码来解决这个问题,但如果问题仍然存在,我将切换到更全面支持的服务:

my $url = "http://www.google.com/ig/api?weather=" .$邮政编码;我的 $tpp = XML::TreePP->new();我的 $tree = $tpp->parsehttp( GET => $url );我的 $city = $tree->{xml_api_reply}->{weather}->{forecast_information}->{city}->{"-data"};除非(定义($city)){print "天气服务当前不可用.
";打开(我的文件,'>/home/swarmp/public_html/status/polyweather.xhtml');打印 MYFILE qq(<?xml version="1.0" encoding="utf-8"?>
);打印 MYFILE qq(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
);打印 MYFILE qq(<html xmlns="http://www.w3.org/1999/xhtml">
);print MYFILE qq(天气不可用!
);打印 MYFILE qq(
);打印 MYFILE qq(

);打印 MYFILE qq(数据供应商目前无法提供天气服务. );打印 MYFILE qq(</p> );打印 MYFILE qq(</body> );打印 MYFILE qq(</html> );关闭我的文件;退出(0);} ...

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

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 was unhandled by user code - The remote server returned an error: 403 Forbidden Exception.

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

Further research and adaptation of suggestions

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

            }
        }

Google article illustrating API error handling

Google API Handle Errors

Thanks to:

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

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

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

Alternative

I found this great open source alternative recently

OpenWeatherMap - Free weather data and forecast API

解决方案

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

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.

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.

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.

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

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

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

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