无法连接到www.googleapis.com的443端口:网络不可达。

4
我正在使用这个库在WordPress网站上获取来自YouTube的视频和直播源。代码链接如下:https://github.com/google/google-api-php-client 这段代码过去一个月一直工作正常,但是突然间开始出现随机的“连接失败”的错误(10次尝试中有2次失败,其余正常)。
以下是我遇到错误的代码部分:
function googleapis_get_live_event_widget(){

    //Youtube Videos and Live Events
    require_once( get_stylesheet_directory() . '/lib/google-api-php-client/src/Google/autoload.php' );
    $GOOGLE_DEVELOPER_KEY = get_field('cce_api_google_developer_key','option');

    $client = new Google_Client();
    $client->setDeveloperKey($GOOGLE_DEVELOPER_KEY);
    $youtube = new Google_Service_YouTube($client);

    try {
        $islive = get_field('cce_live_event','option');

        $opts = array();
        $opts['channelId'] = get_field('cce_api_youtube_channel_id','option');
        if($islive) {
            $opts['eventType'] = 'live';
        }
        $opts['type'] = 'video';
        $opts['maxResults'] = 1;
        $opts['order'] = 'date';


        $live_videos = $youtube->search->listSearch('id,snippet',$opts);

        $data = array();
        $data['live_events'] = $live_videos;
        echo Timber::compile('partials/live_event_widget.twig',$data);

    } catch (Exception $e) {
        $error = array();
        $error['message'] = $e->getMessage();
        echo Timber::compile('partials/error.twig',$error);
        return;
    }


}

add_shortcode('googleapis_live_widget','googleapis_get_live_event_widget');

在服务器上通过ssh执行curl -v https://www.googleapis.com,如果尝试失败,则会出现以下结果:

[~]# curl -v https://www.googleapis.com
* About to connect() to www.googleapis.com port 443 (#0)
*   Trying 216.58.216.202... Connection timed out
*   Trying 2607:f8b0:4009:80b::200a... Failed to connect to     2607:f8b0:4009:80b::200a: Network is unreachable
* Success
* couldn't connect to host
* Closing connection #0
curl: (7) Failed to connect to 2607:f8b0:4009:80b::200a: Network is unreachable

当请求良好时,就像这样:
curl -v https://www.googleapis.com
* About to connect() to www.googleapis.com port 443 (#0)
*   Trying 216.58.216.106... connected
* Connected to www.googleapis.com (216.58.216.106) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_RSA_WITH_AES_128_CBC_SHA
* Server certificate:
*   subject: CN=*.googleapis.com,O=Google Inc,L=Mountain View,ST=California,C=US
*   start date: Oct 21 22:31:24 2015 GMT
*   expire date: Jan 19 00:00:00 2016 GMT
*   common name: *.googleapis.com
*   issuer: CN=Google Internet Authority G2,O=Google Inc,C=US
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: www.googleapis.com
> Accept: */*
> 
< HTTP/1.1 404 Not Found
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: Fri, 01 Jan 1990 00:00:00 GMT
< Date: Sat, 31 Oct 2015 11:33:49 GMT
< Vary: X-Origin
< Content-Type: text/html; charset=UTF-8
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< Server: GSE
< Alternate-Protocol: 443:quic,p=1
< Alt-Svc: quic=":443"; p="1"; ma=604800
< Accept-Ranges: none
< Vary: Origin,Accept-Encoding
< Transfer-Encoding: chunked
< 
* Connection #0 to host www.googleapis.com left intact
* Closing connection #0

此时,如果我运行页面,新闻聚合将正常显示。

为解决这种不一致性,我应该寻找什么?


2
我猜测你的问题是由于防火墙引起的,请参考这个答案http://stackoverflow.com/a/39590519/2652524。 - Gujarat Santana
1
谢谢,问题已通过调整防火墙设置以允许 Google 使用的 IP 范围而解决。 - lithiumlab
如果这个回答对您有帮助,请给它点赞,我们将不胜感激。谢谢。 - Gujarat Santana
2个回答

5

我遇到了同样的错误,正在尝试找出原因。

这个错误有几种可能性。

  1. 你的网络防火墙阻止了与谷歌的连接:请在你的 Laravel 主机上尝试 ping android.googleapis.comcurl -v gcm-http.googleapis.com。如果没有响应,则被阻止了。

  2. 开放谷歌 API 的端口,我通常会打开端口 4435528-2230,你可以在你的防火墙中进行配置。

  3. gcm-http.googleapis.com 有很多 IP 地址,因此你需要允许来自这些 IP 地址的连接,这是其中一个 IP 地址:172.217.3.170。我通过允许其 IP 地址范围之间的连接来解决问题。你可以在防火墙软件中监控 IP 地址。

  4. 你可以在这里这里查找谷歌 API IP 地址的范围。

由于我假设你是在安全网络环境中工作,因此根据你的防火墙配置,你需要与负责配置防火墙的人员进行设置。


-1

我曾经遇到一个用户尝试访问Google Drive API的同样问题。最终发现是因为该应用程序未被允许访问互联网。因此,他在设备上授权了我的应用程序获得该权限,一切又开始正常工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接