我试图建立一个内容管理系统,使用谷歌驱动器帐户作为存储和显示其内容,文件/文件夹,用户应该能够通过我的角度FrontEnd管理这些文件。我是google-api的新手,我想试一试,但我被卡住了。
我正在使用这个库让它在我本地的xampp上用PHP运行。
我尝试在用户文件的.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);但这会抛出致命错误:未捕获Google_Service_Exception:{ "error":{ "errors":[{ "domain":"usageLimits","reason":"dailyLimitExceededUnreg","message":“超过每日未授权使用限制。继续使用需要注册。”,
我让git存储库的示例正常工作。但是我没有找到一个有用的例子来查询用户的文件结构。
发布于 2017-05-13 17:12:34
我发现了我在测试应用程序中缺少的东西:像这样设置访问令牌->
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();
}使用客户端创建服务,如->
$service = new Google_Service_Drive($client);最后,重要的一点是:只有在登录的情况下才能访问它
/************************************************
* 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>";
}
}https://stackoverflow.com/questions/43946445
复制相似问题