【问题标题】:Error when creating folder in google drive在谷歌驱动器中创建文件夹时出错
【发布时间】:2018-03-09 08:25:51
【问题描述】:

我在网络中进行了搜索,但找不到解决方案。请帮忙。下面是代码。

    <?php 

require_once '../../vendor/autoload.php';

define('APPLICATION_NAME', 'Drive API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/drive-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SCOPES', implode(' ', array(
  Google_Service_Drive::DRIVE_METADATA_READONLY)
));

$client = new Google_Client();

$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setScopes(array('https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.apps.readonly', 'https://www.googleapis.com/auth/drive'));
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setAccessType('offline');


$service = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array(
    'name' => 'Test Folder',
    'mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array(
    'fields' => 'id'));
printf("Folder ID: %s\n", $file->id);die();


?>

我可以列出驱动器中的所有文件,但无法创建文件夹。我错过了什么?下面是错误。有人可以帮我解决我的这个问题。提前致谢。

PHP Fatal error:  Uncaught Google_Service_Exception: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

【问题讨论】:

    标签: php google-drive-api google-api-php-client


    【解决方案1】:

    我希望这能帮助您解决问题,我在这里发现了您的错误: https://developers.google.com/drive/v3/web/handle-errors

    为了帮助您找到解决方案,这里是 sn-p,希望这对您有所帮助:

    401:无效的凭据 无效的授权标头。您使用的访问令牌已过期或无效。

    {
      "error": {
        "errors": [
          {
            "domain": "global",
            "reason": "authError",
            "message": "Invalid Credentials",
            "locationType": "header",
            "location": "Authorization",
          }
        ],
        "code": 401,
        "message": "Invalid Credentials"
      }
    }
    

    建议的操作:使用长期刷新令牌刷新访问令牌。如果失败,请引导用户完成 OAuth 流程,如使用 Google Drive 授权您的应用中所述。

    如果您想了解更多关于使用 google drive 授权您的应用程序的信息,请参阅此网站: https://developers.google.com/drive/v3/web/about-auth

    希望您能够利用所提供的资源找到解决方案。

    【讨论】:

    • 我已经修改了代码。我已经访问了谷歌驱动器,因为我可以列出 GDrive 中的所有文件。但是当我创建一个新文件夹时。发生错误。 PHP致命错误:未捕获的Google_Service_Exception:{“error”:{“errors”:[{“domain”:“global”,“reason”:“insufficientPermissions”,“message”:“Insufficient Permission”}],“code”: 403, "消息": "权限不足" } }
    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多