1
votes

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
It looks like a bug (tried looking for any related, didn't found anything), try posting this to their bug tracker.Mr.Rebot
If you can set the authorization for the requests from the other API, you can use the access token you have. Look into service accounts.tehhowch

1 Answers

2
votes

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