首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP google-api-php-client获取用户文件结构

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

Stack Overflow用户
提问于 2017-05-13 05:05:06
回答 1查看 176关注 0票数 0

我试图建立一个内容管理系统,使用谷歌驱动器帐户作为存储和显示其内容,文件/文件夹,用户应该能够通过我的角度FrontEnd管理这些文件。我是google-api的新手,我想试一试,但我被卡住了。

我正在使用这个库让它在我本地的xampp上用PHP运行。

Git Google PHP API

我尝试在用户文件的.php测试文件中获得响应,如下所示:

代码语言:javascript
复制
/*************************************************
 * 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);

但这会抛出致命错误:未捕获Google_Service_Exception:{ "error":{ "errors":[{ "domain":"usageLimits","reason":"dailyLimitExceededUnreg","message":“超过每日未授权使用限制。继续使用需要注册。”,

我让git存储库的示例正常工作。但是我没有找到一个有用的例子来查询用户的文件结构。

EN

回答 1

Stack Overflow用户

发布于 2017-05-13 17:12:34

我发现了我在测试应用程序中缺少的东西:像这样设置访问令牌->

代码语言:javascript
复制
   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();
}

使用客户端创建服务,如->

代码语言:javascript
复制
$service = new Google_Service_Drive($client);

最后,重要的一点是:只有在登录的情况下才能访问它

代码语言:javascript
复制
/************************************************
 * 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>";
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43946445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档