【发布时间】:2013-03-05 21:39:52
【问题描述】:
我跟随THIS TUTORIAL 直接从远程服务器使用 php 在 Google Drive 上上传文件:所以我从 Google API 控制台创建新的 API 项目,启用 Drive API 和 Drive SDK 服务,请求 OAuth 客户端 ID 和客户端密码,并将它们写入脚本,然后将其与Google APIs Client Library for PHP文件夹一起上传到http://www.MYSERVER.com/script1.php,以检索Auth code:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$drive = new Google_Client();
$drive->setClientId('XXX'); // HERE I WRITE MY Client ID
$drive->setClientSecret('XXX'); // HERE I WRITE MY Client Secret
$drive->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$drive->setScopes(array('https://www.googleapis.com/auth/drive'));
$gdrive = new Google_DriveService($drive);
$url = $drive->createAuthUrl();
$authorizationCode = trim(fgets(STDIN));
$token = $drive->authenticate($authorizationCode);
?>
当我访问http://www.MYSERVER.com/script1.php 时,它运行良好,因此我允许授权并获得我可以在第二个脚本中编写的 Auth 代码。然后我把它上传到http://www.MYSERVER.com/script2.php,他看起来像:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$drive = new Google_Client();
$drive->setClientId('X'); // HERE I WRITE MY Client ID
$drive->setClientSecret('X'); // HERE I WRITE MY Client Secret
$drive->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$drive->setScopes(array('https://www.googleapis.com/auth/drive'));
$gdrive = new Google_DriveService($drive);
$_GET['code']= 'X/XXX'; // HERE I WRITE AUTH CODE RETRIEVED AFTER RUNNING REMOTE script.php
file_put_contents('token.json', $drive->authenticate());
$drive->setAccessToken(file_get_contents('token.json'));
$doc = new Google_DriveFile();
$doc->setTitle('Test Drive');
$doc->setDescription('Document');
$doc->setMimeType('text/plain');
$content = file_get_contents('drive.txt');
$output = $gdrive->files->insert($doc, array(
'data' => $content,
'mimeType' => 'text/plain',
));
print_r($output);
?>
好吧,现在我可以在 MYSERVER 的同一个文件夹中上传一个 token.json 空文件(可写)和一个简单的 drive.txt(要上传到我的驱动器中的文件),但是当我最终访问 http://www.MYSERVER.com/script2.php 时,浏览器每次都会给出HTTP 500(内部服务器错误) 和 obv 没有文件上传到我的 Google 云端硬盘:步骤中有错误,或者脚本有问题?请帮我解决这个问题!
编辑:
MYSERVER 错误日志已满:
PHP Fatal error: Uncaught exception 'Google_AuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_grant'' in /var/www/vhosts/.../gdrive/google-api-php-client/src/auth/Google_OAuth2.php:113
Stack trace:
#0 /var/www/vhosts/.../gdrive/google-api-php-client/src/Google_Client.php(131): Google_OAuth2->authenticate(Array, NULL)
#1 /var/www/vhosts/.../gdrive/sample2.php(23): Google_Client->authenticate()
#2 {main}
thrown in /var/www/vhosts/.../gdrive/google-api-php-client/src/auth/Google_OAuth2.php on line 113
【问题讨论】:
-
检查您的服务器日志以了解实际错误。
-
你的代码现在看起来不错,检查你的错误日志是否有错误,还要仔细检查文件权限,如果脚本没有写入文件的权限会触发错误(现在看来唯一可能的问题)
-
访问日志。检查错误日志。
-
@John : 那些* :p 谢谢你的笑声! +1
-
Huxley,伙计,这些输入来自 ACCESS 日志,现在对我们来说已经没什么用了,我们需要可以在 error_log 中找到的 ERROR 日志
标签: php google-api google-drive-api google-api-php-client