简体   繁体   English

PHP google-api-php-client获取用户文件结构

[英]PHP google-api-php-client getting user File structure

i am trying to build a CMS which uses a Google Drive Account as storage and display its content, files/folders and the user shall be able to manage those files via my Angular FrontEnd. 我正在尝试建立一个使用Google云端硬盘帐户存储并显示其内容,文件/文件夹的CMS,并且用户应能够通过我的Angular FrontEnd管理那些文件。 I am new to the google-api and thought i give it a try but i'm stuck. 我是google-api的新手,并认为我可以尝试一下,但是遇到了麻烦。

I am using this library to get it working in PHP on my local xampp. 我正在使用此库来使其在本地xampp上的PHP中运行。

Git Google PHP API Git Google PHP API

I try to get a response in a .php test file of the users files like this: 我尝试在用户文件的.php测试文件中获得响应,如下所示:

/*************************************************
 * Ensure you've downloaded your oauth credentials
 ************************************************/
if (!$oauth_credentials = getOAuthCredentialsFile())
{
    echo missingOAuth2CredentialsWarning();
    return;
}

/************************************************
 * The redirect URI is to the current page, e.g:
 * http://localhost:8080/simple-file-upload.php
 ************************************************/
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

$client = new Google_Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope(Google_Service_Drive::DRIVE);
$service = new Google_Service_Drive($client);


$pageToken = null;
do {
    $response = $service->files->listFiles();

    foreach ($response->files as $file) {
        printf("Found file: %s (%s)\n", $file->name, $file->id);
    }
} while ($pageToken != null);

But this throws Fatal error: Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "usageLimits", "reason": "dailyLimitExceededUnreg", "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.", 但这会引发致命错误:未捕获的Google_Service_Exception:{“错误”:{“错误”:[{“域”:“ usageLimits”,“原因”:“ dailyLimitExceededUnreg”,“消息”:“对未经身份验证的使用的每日限制已超过。续。使用需要注册。”,

I got the examples of the git repository to work. 我得到了git存储库的示例。 But i didn't find a useful example to just query the users file structure. 但是我没有找到一个有用的例子来查询用户的文件结构。

I found what i was missing in my test application: Setting access tokens like this -> 我发现我的测试应用程序中缺少什么:设置访问令牌,如下所示->

   if (isset($_GET['code']))
{
    $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
    $client->setAccessToken($token); //really needed? we also set it when the session is not empty 

    // store in the session also
    $_SESSION['upload_token'] = $token;

    // redirect back to the example
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

// set the access token as part of the client
if (!empty($_SESSION['upload_token']))
{
    $client->setAccessToken($_SESSION['upload_token']);
    if ($client->isAccessTokenExpired())
    {
        unset($_SESSION['upload_token']);
    }
}
else
{
    $authUrl = $client->createAuthUrl();
}

create a service with the client like -> 与客户端创建服务,例如->

$service = new Google_Service_Drive($client);

And finally, the important part: Only access it, if we are logged in 最后,重要的部分:只有登录后才能访问

/************************************************
 * If we're signed in then lets try to do something
 ************************************************/
if ( $client->getAccessToken())
{
    $response = $service->files->listFiles();

    foreach ($response->files as $file)
    {
        echo "<div>" . $file->name . " - " . $file->id . "</div><br>";
    }
}

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

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