如何使用PHP和google Drive API创建文件夹,并立即更改共享权限?

如何使用PHP和google Drive API创建文件夹,并立即更改共享权限?,php,google-drive-api,Php,Google Drive Api,我已经成功地创建了一个文件夹,但我想知道如何共享它。要使用权限.create功能,我需要文件ID,但我不知道在哪里可以找到它 因此,我基本上可以创建文件夹并立即设置共享权限,还是需要文件ID?如果是的话,我该怎么做?(这可能适用于任何类型的文件,而不仅仅是文件夹) 这是我必须创建文件夹的代码。我省略了创建google客户端对象和进行身份验证的代码,因为我认为这与此无关 $drive = new Google_Service_Drive($client); $fileA = ne

我已经成功地创建了一个文件夹,但我想知道如何共享它。要使用
权限.create
功能,我需要
文件ID
,但我不知道在哪里可以找到它

因此,我基本上可以创建文件夹并立即设置共享权限,还是需要文件ID?如果是的话,我该怎么做?(这可能适用于任何类型的文件,而不仅仅是文件夹)

这是我必须创建文件夹的代码。我省略了创建google客户端对象和进行身份验证的代码,因为我认为这与此无关

    $drive = new Google_Service_Drive($client);

    $fileA = new Google_Service_Drive_DriveFile();
    $fileA->setName($Title);
    $fileA->setMimeType('application/vnd.google-apps.folder');


    $folder = $drive->files->create($fileA);
表示“create”方法返回文件资源(即表示新文件的对象)

如果查看Files资源()的定义,您将看到它有一个名为“id”的属性

因此,我希望名为
$folder
的变量将有一个
id
属性,您可以使用它

在PHP中,通常会得到如下属性:
$folder->id
。所以你可以写
echo$folder->id在下一行,只是为了测试它。或者您可以编写
var\u dump($folder)如果您想查看对象的所有属性,并检查其中的内容

表示“create”方法返回文件资源(即表示新文件的对象)

如果查看Files资源()的定义,您将看到它有一个名为“id”的属性

因此,我希望名为
$folder
的变量将有一个
id
属性,您可以使用它


在PHP中,通常会得到如下属性:
$folder->id
。所以你可以写
echo$folder->id在下一行,只是为了测试它。或者您可以编写
var\u dump($folder)如果您想查看对象的所有属性,并检查其中的内容

您需要打两个电话。创建文件/文件夹时,所有者设置为当前经过身份验证的用户。一旦您获得了所创建的新文件/目录的文件id,则可以在调用中使用该id来创建对该文件的其他权限

// Option paramaters can be set as needed.
 $optParams = array(

  'emailMessage' => '[YourValue]',  // A custom message to include in the notification email.

  'sendNotificationEmail' => '[YourValue]',  // Whether to send a notification email when sharing to users or groups. This defaults to true for users and groups, and is not allowed for other requests. It must not be disabled for ownership transfers.

  'supportsTeamDrives' => '[YourValue]',  // Whether the requesting application supports Team Drives.

  'transferOwnership' => '[YourValue]',  // Whether to transfer ownership to the specified user and downgrade the current owner to a writer. This parameter is required as an acknowledgement of the side effect.
  'fields' => '*'
);
// Single Request.
$results = permissionsCreateExample($service, $fileId, $optParams);


/**
* Creates a permission for a file or Team Drive.
* @service Authenticated Drive service.
* @optParams Optional paramaters are not required by a request.
* @fileId The ID of the file or Team Drive.
* @return Permission
*/
function permissionsCreateExample($service, $body, $fileId, $optParams)
{
    try
    {
        // Make the request and return the results.
        return $service->permissions->CreatePermissions($body, $fileId,  $optParams);
    }
    catch (Exception $e)
    {
        print "An error occurred: " . $e->getMessage();
    }
}

你需要打两个电话。创建文件/文件夹时,所有者设置为当前经过身份验证的用户。一旦您获得了所创建的新文件/目录的文件id,则可以在调用中使用该id来创建对该文件的其他权限

// Option paramaters can be set as needed.
 $optParams = array(

  'emailMessage' => '[YourValue]',  // A custom message to include in the notification email.

  'sendNotificationEmail' => '[YourValue]',  // Whether to send a notification email when sharing to users or groups. This defaults to true for users and groups, and is not allowed for other requests. It must not be disabled for ownership transfers.

  'supportsTeamDrives' => '[YourValue]',  // Whether the requesting application supports Team Drives.

  'transferOwnership' => '[YourValue]',  // Whether to transfer ownership to the specified user and downgrade the current owner to a writer. This parameter is required as an acknowledgement of the side effect.
  'fields' => '*'
);
// Single Request.
$results = permissionsCreateExample($service, $fileId, $optParams);


/**
* Creates a permission for a file or Team Drive.
* @service Authenticated Drive service.
* @optParams Optional paramaters are not required by a request.
* @fileId The ID of the file or Team Drive.
* @return Permission
*/
function permissionsCreateExample($service, $body, $fileId, $optParams)
{
    try
    {
        // Make the request and return the results.
        return $service->permissions->CreatePermissions($body, $fileId,  $optParams);
    }
    catch (Exception $e)
    {
        print "An error occurred: " . $e->getMessage();
    }
}

表示“create”方法返回文件资源(即表示新文件的对象)。如果查看Files资源()的定义,您将看到它有一个名为“id”的属性。因此,我希望您非常奇怪地命名为“$folder”的变量将有一个“id”属性,您可以使用它。哇,好的,谢谢。如何获取Id属性?正如我所说,它是
$folder
对象的属性。因此,在PHP中,通常会得到如下属性:
$folder->id
。所以你可以写
echo$folder->id在下一行,只是为了测试它。或者您可以编写
var\u dump($folder)如果您想查看对象的所有属性,并查看其中的内容。我想继续并表达我最诚挚的感谢。它起作用了。我已经被困了好几个小时,现在多亏了你,我再也不会这样了。非常非常感谢,祝你有一个美好的一天!没问题。我把它写在下面,作为一个正确的答案,如果您愿意将它标记为已接受,谢谢。另外,文档永远是你的朋友!表示“create”方法返回文件资源(即表示新文件的对象)。如果查看Files资源()的定义,您将看到它有一个名为“id”的属性。因此,我希望您非常奇怪地命名为“$folder”的变量将有一个“id”属性,您可以使用它。哇,好的,谢谢。如何获取Id属性?正如我所说,它是
$folder
对象的属性。因此,在PHP中,通常会得到如下属性:
$folder->id
。所以你可以写
echo$folder->id在下一行,只是为了测试它。或者您可以编写
var\u dump($folder)如果您想查看对象的所有属性,并查看其中的内容。我想继续并表达我最诚挚的感谢。它起作用了。我已经被困了好几个小时,现在多亏了你,我再也不会这样了。非常非常感谢,祝你有一个美好的一天!没问题。我把它写在下面,作为一个正确的答案,如果您愿意将它标记为已接受,谢谢。另外,文档永远是你的朋友!