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

在php中使用google drive api列出所有文件时出错,php,google-api,google-drive-api,google-oauth,google-api-php-client,Php,Google Api,Google Drive Api,Google Oauth,Google Api Php Client,我正在使用PHP开发GoogleDriveAPI。我在使用此驱动器api时遇到错误。我正在使用google api客户端库“google/apiclient^2.0”。我正在尝试使用google身份验证进行用户登录,并查看驱动器中的所有可用文件。为此,我正在使用此库,但它显示错误: 注意:未定义的属性:Google_客户端::$files in /第31行的Applications/XAMPP/xamppfiles/htdocs/google_app/index.php 致命错误:未捕获错误:调

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

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

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

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

提前谢谢

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

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

你可以考虑一下这些线< /P>
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();
    }
}
从我的示例项目中删除的代码

未定义的属性Google_客户端

表示您尚未正确声明google客户端

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


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

这个错误解释得很清楚。您尝试读取错误了吗?现在它给出了新的错误“超出未经验证使用的每日限制”?为什么会发生这种情况?我只尝试我的代码4或5次。我怎样才能摆脱这个错误太棒了!!谢谢@DalmTo。还有一件事,您能告诉我如何在addscope函数$client->addscope(“scope3”)中定义多个作用域吗;将scope3附加到您的作用域中$客户端->添加范围(数组(“范围4”、“范围5”);将添加多个。感谢您的帮助@DalmToGot执行某些操作后出现错误,表示必须传递刷新令牌或将其设置为setAccessToken的一部分。您可能希望签出该示例文件的另一半
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();
    }
}