调用Google API时无法连接错误(在驱动器中最常出现)

时间:2014-02-03 19:20:47

标签: google-drive-api google-api-php-client google-compute-engine

星期五,我使用的是Google Drive API PHP客户端版本0.6,我开始收到很多看似

的错误

PHP致命错误:未捕获的异常'Google_IOException',消息'HTTP错误:(0)无法连接到2607:f8b0:4001:c03 :: 54:网络无法访问

我升级到1.0版本,但我仍然得到以下内容:  (0)HTTP错误:无法连接

无法打开流:连接超时

在各种场合。

是否应该捕获这些错误?在https://developers.google.com/drive/web/handle-errors中没有提到像这样的IO错误......

更新: 在Google Compute Engine上运行这一切,所以我更新了标签,以防这是一个GCE问题?

以下是我周五使用0.6客户端库的示例代码 - 其中$ file_contents显然是我文件的内容...看起来像这个代码在星期四工作正常并且错误没有定期发生

(驱动对象传递给函数):

  $file = new Google_DriveFile($drive->files->get($file_id));

      $additionalParams = array(
              'data' => $file_contents
          );
      try{
        $update_result = $drive->files->update($file_id,$file,$additionalParams);
        if($update_result){
          return "true";
        }else{
          return "Error: Could not update file, please try again";
        }
      }catch (Google_ServiceException $e) {
        return "Error: Could not create file, please try again (Google Error) " . $e->getMessage();
      } catch (Google_Exception $e) {
        return "Error: Could not create file, please try again (Google Error) " . $e->getMessage();
      }catch (Exception $e) {
        return "Error: " . $e->getMessage();
      }

和我使用1.0的更新代码

    include_once 'gapi_php_client_1/src/Google/Client.php';
    include_once 'gapi_php_client_1/src/Google/Service/Drive.php';
    global $session_access_token;
    global $session_expires_on;
    global $session_refresh_token;
    global $client_id;
    global $client_secret;

    $client = new Google_Client();
    $client->setApplicationName('Google+ PHP Starter Application');
    //// Visit https://code.google.com/apis/console?api=plus to generate your
    //// client id, client secret, and to register your redirect uri.
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri('postmessage');
    $client->setAccessToken($session_access_token);
    $client->refreshToken($session_refresh_token);

    try{

      $file = $drive->files->get($file_id);
      $additionalParams = array(
              'data' => $file_contents,
                'uploadType' => 'multipart'
          );
      $update_result = $drive->files->update($file_id,$file,$additionalParams);

    }catch (Google_ServiceException $e) {
       $file_save = false;
    }catch (Google_IOException $e) {
      $file_save = false;
    } catch (Google_Exception $e) {
      $file_save = false;
    }catch (Exception $e) {
      $file_save = false;
    }

1 个答案:

答案 0 :(得分:0)

如果在幕后使用fiddler或wireshark,你也可能面临这个错误.....去任务经理并杀死fiddler / wireshark

它对我有用....