首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在远程服务器上使用google php客户端时HTTP 500 (内部服务器错误)

问在远程服务器上使用google php客户端时HTTP 500 (内部服务器错误)
EN

Stack Overflow用户
提问于 2013-03-05 21:39:52
回答 1查看 11.5K关注 0票数 1

我跟随本教程使用php直接从远程服务器上传一个文件到Google上:因此我从Google控制台创建了新的API,启用了Drive和Drive服务,请求OAuth客户端ID和客户端秘密,并将它们写入脚本,然后将它们与Google客户端PHP库文件夹一起上传到http://www.MYSERVER.com/script1.php,以检索Auth代码:

代码语言:javascript
复制
<?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,他看起来像:

代码语言:javascript
复制
<?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浏览器时,每次在Google中上传HTTP 500 (内部服务器错误)和obv时,都没有文件被上传:步骤中有错误,或者脚本有问题?请帮我解决这个问题!

编辑:

MYSERVER错误日志中充满了:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-05 21:55:07

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

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

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

要撤销您的第一个访问令牌,请转到Google控制台,您将在客户端ID下找到它,用于安装的应用程序

诚挚的问候

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15234668

复制
相关文章

相似问题

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