繁体   English   中英

在php中使用google drive api列出所有文件时出错

[英]error on listing all files using google drive api in php

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

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

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

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 检索 Google 驱动器中的所有文件,然后设置特定文件的状态自动发布。 请帮助我摆脱这个错误。

提前致谢。

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

意味着您在未正确验证脚本的情况下发出请求。 您的 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

意味着你没有正确声明谷歌客户端

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM