douxiluan6555 2018-10-12 14:21
浏览 72

谷歌驱动器API(团队驱动器)文件权限在PHP中的任何人或特定域

On the Google API explorer I tried giving the correct fileId in drive.permissions.create. I set supportsTeamDrives to true and Request body with role: reader and type: anybody. This just gives me the error:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalidLinkVisibility",
    "message": "Bad Request. User message: \"\""
   }
  ],
  "code": 400,
  "message": "Bad Request. User message: \"\""
 }
}

Same goes in my PHP code. Here is as close as I can get, that looks right to me:

...
$userPermission     = new Google_Service_Drive_Permission( array(
                'type'          => 'anyone',
                'role'          => 'reader',
            ));
            $service->permissions->create( $imageId, $userPermission, array(
                'supportsTeamDrives'    => true,
            ));
...

I can't find anything on the error:

invalidLinkVisibility

What I need to do is to temporarily set the permission on 20-50 files in a folder to "public" (i.e. anyone with the link can see it) because I'm sending the webContentLink (or webViewLink, I haven't figured out which one I need to use yet) to another API to upload the images there. Once this is done I'd like to remove the public access. One option would be to set the permission (with recursive public access) on the folder to public access. I just can't figure out how I change (or rather add) permissions to a file in the Google Drive API, in PHP.

An alternative to this would be if there is some way to get the image thru a URL with the token included, e.g.

$source     = $files.$image1;
$context    = stream_context_create( array(
    'https' => array(
        'header'  => 'Authorization: Bearer '. $token
    )
) );
$image = file_get_contents( $source, false, $context );

Where $token is the the auth token from authenticating the service, $files is https://drive.google.com/a/file/d/ and $image1 is the id of the file. Of course when trying this I have to first logon to my Google account, which won't work since I'll have another API calling these links and posting the content to another service. Is there some way to make this work? This way I wouldn't have to worry about the file permissions (back-n-forth).

  • 写回答

1条回答 默认 最新

  • dsh125986083 2019-08-11 13:31
    关注

    you need to delete the old permissions object first , this object will have this default initial id usually : 10933160239610460256k (i found this id when i was trying to figure out this solution through analysing GDrive frontend-backend interactions)

    $service->permissions->delete($file->id, '10933160239610460256k',array('supportsTeamDrives'=>true));
    

    then create a new permission like this

    $Permission = new \Google_Service_Drive_Permission(array(
        'type' => 'anyone',
        'role' => "reader",
        'additionalRoles' => [],
        'withLink' => true,
        'value' =>'YOUR_TEAM_DRIVE_DOMAIN' 
              ));
    $service->permissions->create(
            $file->id, $Permission, array('fields' => 'id','supportsTeamDrives'=>true));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探