【问题标题】:HTTP 500 (Internal Server Error) when using google api php client on a remote server在远程服务器上使用 google api php 客户端时出现 HTTP 500(内部服务器错误)
【发布时间】: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


【解决方案1】:

您似乎无法获得新的访问令牌,因为您的旧令牌尚未过期,这就是这段代码告诉您的:

'Google_AuthException' 带有消息'获取 OAuth2 访问令牌时出错,消息:'invalid_grant'

您必须获得一次访问令牌,将其保存在某处并使用它直到过期,每次尝试查询某些内容时都无法获得新的访问令牌

要撤销您的第一个访问令牌,请转到 Google API Console,您会在 已安装应用程序的客户端 ID

下找到它

最好的问候

【讨论】:

  • 不幸的是我已经检查过了:token.json 有 666 权限,script2.php 有 766(或 777),但我得到同样的错误..
  • 目录权限呢?目录可写吗?
  • 当然,www.MYSERVER.com/gdrive 目录有 777 权限:(
  • 您的日志,除了未捕获的异常,之前还有什么吗?应该有一些关于文件权限的东西..
  • 好的,有点晚了,这是艰难的一天,我没有正确阅读错误,但现在我阅读了,请查看更新的答案,因为我们将在不同的方式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 2019-03-10
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
相关资源
最近更新 更多