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

在远程服务器上使用google api php客户端时出现HTTP 500(内部服务器错误),php,google-api,google-drive-api,google-api-php-client,Php,Google Api,Google Drive Api,Google Api Php Client,我随后用php直接从远程服务器上传了一个文件到Google Drive:因此我从Google API控制台创建了一个新的API项目,启用了驱动器API和驱动器SDK服务,请求OAuth客户端ID和客户端密码,并将其写入脚本,然后将其与文件夹一起上传到,以检索身份验证代码: <?php require_once 'google-api-php-client/src/Google_Client.php'; require_once 'google-api-php-client/src/con

我随后用php直接从远程服务器上传了一个文件到Google Drive:因此我从Google API控制台创建了一个新的API项目,启用了驱动器API和驱动器SDK服务,请求OAuth客户端ID和客户端密码,并将其写入脚本,然后将其与文件夹一起上传到,以检索身份验证代码:

<?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);

?>

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

“Google_AuthException”,消息为“获取OAuth2访问令牌时出错,消息为:“无效\u授权”

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

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


致以最诚挚的问候

检查您的服务器日志,了解实际错误是什么。您的代码目前看起来不错,检查您的错误日志,同时仔细检查文件权限。如果脚本没有权限写入属于访问日志的文件(这似乎是目前唯一可能的问题),则会触发错误。检查错误日志。@John:那些*:p,谢谢你的好笑+赫胥黎,我的伙计,这些输入来自访问日志,现在对我们来说毫无用处,我们需要错误日志,可以在错误日志中找到。不幸的是,我已经检查过了:token.json有666权限,script2.php有766(或777),但我得到了相同的错误。那么目录权限呢?目录是否可写?当然,www.MYSERVER.com/gdrive目录具有777权限:(除了未捕获的异常之外,您的日志之前还有什么吗?应该有一些关于文件权限或其他方面的信息..好的,有点晚了,这是一个艰难的一天,我没有正确阅读错误,但现在我阅读了,请查看更新的答案,因为我们将以不同的方式处理错误
<?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);

?>
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