[英]How can I define a Google_Service_Drive variable correctly with Google Drive API using PHP to create a folder?
I'm newbie on this, so I hope you could help me, I'm checking the Google Drive API docs because I want to create a folder in Google Drive using PHP, I'm using the code example but I notice that I must define this "$driveService" variable first. 我是新手,因此希望您能对我有所帮助,我正在检查Google Drive API文档,因为我想使用PHP在Google Drive中创建一个文件夹,我使用的是代码示例,但我注意到我必须首先定义此“ $ driveService”变量。
I must say I made the previous steps before (Create a project, install via composer, generate my client_secret.json file, etc). 我必须说我之前已经完成了前面的步骤(创建项目,通过composer安装,生成我的client_secret.json文件等)。
So I tried to use the example writing this code: 因此,我尝试使用编写以下代码的示例:
<?php
require_once __DIR__.'/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfigFile('64*****-client_secret.json');
$client->setRedirectUri('https://www.mywebsite.com/drive/oauth2callback.php');
$client->addScope(Google_Service_Drive::DRIVE);
$driveService = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'TEST',
'mimeType' => 'application/vnd.google-apps.folder'));
$file = $driveService->files->create($fileMetadata, array('fields' => 'id'));
printf("Folder ID: %s\n", $file->id);
?>
But when I execute the page it shows nothing in the browser, it shows this in terminal: 但是,当我执行该页面时,它在浏览器中什么也没显示,而是在终端中显示:
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with
message '{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
' in /var/www/mywebsite/public_html/drive/vendor/google/apiclient/src/Google/Http/REST.php:118
Stack trace: #0 /var/www/mywebsite/public_html/drive/vendor/google/apiclient/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /var/www/mywebsite/public_html/drive/vendor/google/apiclient/src/Google/Task/Runner.php(176): call_user_func_array(Array, Array) #3 /var/www/mywebsite/public_html/drive/vendor/google/apiclient/src/Google/Http/REST.php(58): Google_Task_Runner->run() in /var/www/mywebsite/public_html/drive/vendor/google/apiclient/src/Google/Http/REST.php on line 118
I also added these lines: 我还添加了以下几行:
$client = new Google_Client();
$client->setAuthConfigFile('64*****-client_secret.json');
$client->setRedirectUri('https://www.mywebsite.com/drive/oauth2callback.php');
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->addScope(Google_Service_Drive::DRIVE);
But it's still the same, I can't create the folder and get the folder ID. 但是仍然一样,我无法创建文件夹并获取文件夹ID。 How can I fix it?
我该如何解决?
I'll appreciate your answer. 谢谢您的回答。
You need to get the access token first; 您需要先获取访问令牌; the user needs to grant you access to his or her drive file through the user consent screen.
用户需要通过用户同意屏幕授予您对其驱动器文件的访问权限。 Yo can try this;
哟可以试试看;
$client = new Google_Client();
$client->setAuthConfigFile('64*****-client_secret.json');
$client->setRedirectUri('https://www.mywebsite.com/drive/oauth2callback.php');
$client->addScope(Google_Service_Drive::DRIVE);
/** checks if the access token has previously been obtained and sets it **/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']){
$client->setAccessToken($_SESSION['access_token']);
//what ever code you want to run
else{
//redirects the user to authenticate
$redirect_uri = 'https://www.mywebsite.com/drive/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
}
The $client->setAuthConfig('credentials.json'); $ client-> setAuthConfig('credentials.json'); you added is a repetition and not necessary.
您添加的是重复项,没有必要。 Also remember to call session start at the beginning of the code like this:session_start();
同样要记住在这样的代码开头调用会话开始:session_start();
For detail check out: https://developers.google.com/identity/protocols/OAuth2WebServer 有关详细信息,请访问: https : //developers.google.com/identity/protocols/OAuth2WebServer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.