代码之家  ›  专栏  ›  技术社区  ›  NekoLopez

如何使用PHP创建文件夹,通过google drive api正确定义google_service_drive变量?

  •  1
  • NekoLopez  · 技术社区  · 7 年前

    我是这方面的新手,所以我希望你能帮助我,我正在检查 Google Drive API docs 因为我想用PHP在Google Drive中创建一个文件夹,所以我使用了这个代码示例,但我注意到我必须首先定义这个“$driveservice”变量。

    我必须说我之前已经完成了前面的步骤(创建一个项目,通过composer安装,生成我的客户机\secret.json文件等)。

    因此我尝试使用编写此代码的示例:

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

    但当我执行页面时,它在浏览器中不显示任何内容,而是在终端中显示:

    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
    

    我还添加了以下行:

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

    但还是一样,我无法创建文件夹并获取文件夹ID。如何修复?

    谢谢你的回答。

    1 回复  |  直到 6 年前
        1
  •  0
  •   cooljay    6 年前

    您需要先获取访问令牌;用户需要通过用户同意屏幕授予您访问他或她的驱动器文件的权限。 你可以试试这个;

     $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));
        }
    
        }
    

    您添加的$client->setauthconfig('credentials.json');是重复的,不需要。还记得在代码的开头调用session start,如:session_start();

    详情请查看: https://developers.google.com/identity/protocols/OAuth2WebServer

    推荐文章