HTTP 500(内部服务器错误)在远程服务器上使用google api php客户端时 [英] HTTP 500 (Internal Server Error) when using google api php client on a remote server

查看:240
本文介绍了HTTP 500(内部服务器错误)在远程服务器上使用google api php客户端时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循此教程上传使用PHP直接从REMOTE SERVER获得文件:因此,我从Google API Console创建新的API项目,启用Drive API和Drive SDK服务,请求OAuth客户端ID和客户端密钥,然后将其写入脚本,然后将其上传与 Google API客户端库PHP 文件夹一起 http://www.MYSERVER.com/script1.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('XXX'); //这里我写了我的客户ID

$ drive-> setClientSecret('XXX'); //这里我写了我的客户端秘密

$ 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'); //这里我写我的客户ID
$ drive-> setClientSecret('X'); //这里我写了我的客户端秘密
$ 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'; //这里我在运行REMOTE后返回AUTH代码RETRIEVED 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致命错误:Uncaught异常'Google_AuthException'带有消息'Error fetching OAuth2 access token,message:'invalid_grant' 'in /var/www/vhosts/.../gdrive/google-api-php-client/src/auth/Google_OAuth2.php:113 
堆栈跟踪:
#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}
抛出/var/www/vhosts/.../gdrive/google-api- php-client / src / auth / Google_OAuth2.php on line 113


解决方案

看起来你无法获得新的访问令牌,因为你的旧访问令牌还没有过期,这就是这段代码告诉你的:



'Google_AuthException'带有消息'错误fet清OAuth2访问令牌,消息:'invalid_grant'



您必须获取访问令牌ONCE,将其保存到某个地方并使用它,直到它到期为止,您将无法获得新的访问令牌每次尝试查询某些内容时,都需要

撤消您的第一个访问令牌,请转至 Google API控制台,您可以在已安装应用程序的客户端ID下找到它



p>

I followed THIS TUTORIAL to upload a file on Google Drive with php, directly from a REMOTE SERVER: so I create new API Project from Google API Console, enable Drive API and Drive SDK services, request OAuth Client ID and Client Secret, and write them in a script, then upload it together with Google APIs Client Library for PHP folder to http://www.MYSERVER.com/script1.php, to retrieve 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);

?>

When I visit http://www.MYSERVER.com/script1.php it run perfectly, so I allow authorization and get the Auth code that I can write in a second script. Then I upload it to http://www.MYSERVER.com/script2.php, who looks like:

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

?>

Well, now I can upload in the same folder of MYSERVER a token.json empty file (writable) and a simple drive.txt (file to upload in my Drive), but when I finally visit http://www.MYSERVER.com/script2.php browser gives everytime a HTTP 500 (Internal Server Error) and obv no file is uploaded in my Google Drive: there are errors in the steps, or some issue with the scripts? Please help me to resolve this!

EDIT:

MYSERVER error log is full of:

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

解决方案

it seems that you cannot get new access token because your old one has not expired yet, that is what's this code is telling you :

'Google_AuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_grant'

You must get the access token ONCE, save it somewhere and use it until it expires, you can not get new access token each time you try to query something

to revoke your first access token go to Google API Console and you will find it under Client ID for installed applications

Best Regards

这篇关于HTTP 500(内部服务器错误)在远程服务器上使用google api php客户端时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆