【问题标题】:error on listing all files using google drive api in php在 php 中使用 google drive api 列出所有文件时出错
【发布时间】:2018-02-12 07:22:00
【问题描述】:

我正在使用 PHP 开发 google drive api。使用此驱动器 api 时遇到错误。我正在使用 google api 客户端库“google/apiclient ^2.0”。我正在尝试使用谷歌身份验证用户登录并查看驱动器中所有可用的文件,我正在使用这个库,但它显示错误:

注意:未定义的属性:Google_Client::$files in /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php 在第 31 行

致命错误:未捕获的错误:调用成员函数 listFiles() on /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php:31 中为空 堆栈跟踪:#0 /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php(55): retrieveAllFiles(Object(Google_Client)) #1 {main} 抛出 /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php 在第 31 行

include_once   'vendor/autoload.php';


function retrieveAllFiles($service) {
  $result = array();
  $pageToken = NULL;

  do {
    try {
      $parameters = array();
      if ($pageToken) {
        $parameters['pageToken'] = $pageToken;
      }
      $files = $service->files->listFiles($parameters);

      $result = array_merge($result, $files->getItems());
      $pageToken = $files->getNextPageToken();
    } catch (Exception $e) {
      print "An error occurred: " . $e->getMessage();
      $pageToken = NULL;
    }
  } while ($pageToken);
  return $result;
}


$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setAccessType("offline");        // offline access
//$client->setIncludeGrantedScopes(true);   // incremental auth
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client->setRedirectUri($redirect_uri);
if (isset($_GET['code'])) {
    $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
}
$service = new Google_service_Device();
echo retrieveAllFiles($service );

实际上,我正在尝试使用我的文件 ID 检索我的谷歌驱动器中的所有文件,然后设置状态自动发布特定文件。请帮我摆脱这个错误。

提前致谢。

【问题讨论】:

  • 错误解释得很清楚。你试过阅读错误吗?
  • 现在出现新错误“超出未经身份验证的使用的每日限制”?为什么会这样?我只尝试我的代码 4 或 5 次。我怎样才能摆脱这个错误

标签: php google-api google-drive-api google-oauth google-api-php-client


【解决方案1】:

“超出未经身份验证的使用的每日限制”

表示您在未正确验证脚本的情况下发出请求。您的 fetchAccessTokenWithAuthCode 可能有问题。

你可能会考虑这些行很长的东西

function getOauth2Client() {
    try {

        $client = buildClient();

        // Set the refresh token on the client. 
        if (isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
            $client->refreshToken($_SESSION['refresh_token']);
        }

        // If the user has already authorized this app then get an access token
        // else redirect to ask the user to authorize access to Google Analytics.
        if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {

            // Set the access token on the client.
            $client->setAccessToken($_SESSION['access_token']);                 

            // Refresh the access token if it's expired.
            if ($client->isAccessTokenExpired()) {              
                $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
                $client->setAccessToken($client->getAccessToken()); 
                $_SESSION['access_token'] = $client->getAccessToken();              
            }           
            return $client; 
        } else {
            // We do not have access request access.
            header('Location: ' . filter_var( $client->getRedirectUri(), FILTER_SANITIZE_URL));
        }
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}

从我的示例项目oauth2php中提取的代码

未定义的属性 Google_Client

表示你没有正确声明 google 客户端

未捕获的错误:调用成员函数 listFiles() on null in

如果您删除 $parameters 并调用 $service->files->listFiles(); 会发生什么

【讨论】:

  • 伟大的陷阱!谢谢@DalmTo。还有一件事你能告诉我如何在 addscope 函数中定义多个范围
  • $client->addScope("scope3");会将 scope3 附加到您的范围内。 $client->addScope(array("scope4", "scope5"));将添加不止一个。
  • 感谢您的帮助!! @DalmTo
  • 经过一些工作后出现错误,提示必须传递刷新令牌或将其设置为 setAccessToken 的一部分。
  • 您可能想查看该示例文件的另一半 github.com/LindaLawton/Google-APIs-PHP-Samples/blob/master/…
猜你喜欢
  • 2016-10-25
  • 2021-05-31
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
相关资源
最近更新 更多