简体   繁体   English

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

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

I am working on google drive api by using PHP.我正在使用 PHP 开发 google drive api。 I am facing an error while using this drive api.我在使用此驱动器 api 时遇到错误。 I am using google api client library "google/apiclient ^2.0".我正在使用 google api 客户端库“google/apiclient ^2.0”。 I am trying to user login with google authentication and view all files available in drive for this purpose, i am using this library but it's showing error:我正在尝试使用 google 身份验证用户登录并为此查看驱动器中可用的所有文件,我正在使用此库,但它显示错误:

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

Fatal error: Uncaught Error: Call to a member function listFiles() on null in /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php:31 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php(55): retrieveAllFiles(Object(Google_Client)) #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/google_app/index.php on line 31致命错误:未捕获的错误:调用成员函数 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 );

Actually i am trying to retrieve all files in my google drive with my file id and after that set status auto publish of specific files.实际上,我正在尝试使用我的文件 ID 检索 Google 驱动器中的所有文件,然后设置特定文件的状态自动发布。 Please help me to get rid of this error.请帮助我摆脱这个错误。

Thanks in advance.提前致谢。

"Daily limit for unauthenticated Use Exceeded" “已超出未经身份验证的使用的每日限制”

Means that you are making a request without properly authenticating your script.意味着您在未正确验证脚本的情况下发出请求。 there is probably something up with your fetchAccessTokenWithAuthCode.您的 fetchAccessTokenWithAuthCode 可能有问题。

You might consider something long these lines你可能会考虑一些很长的这些行

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

Code ripped from my sample project oauth2php从我的示例项目oauth2php 中提取的代码

Undefined property Google_Client未定义的属性 Google_Client

Means you havent properly declared the google client意味着你没有正确声明谷歌客户端

Uncaught Error: Call to a member function listFiles() on null in未捕获的错误:在 null 上调用成员函数 listFiles()

what happens if you remove $parameters and just call $service->files->listFiles();如果删除 $parameters 并调用 $service->files->listFiles() 会发生什么;

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

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