Google Drive SDK

Permissions: patch

Requires authorization

Updates a permission. This method supports patch semantics. Try it now or see an example.

Warning: Concurrent permissions operations on the same file are not supported; only the last update is applied.

Request

HTTP request

PATCH https://www.googleapis.com/drive/v2/files/fileId/permissions/permissionId

Parameters

Parameter name Value Description
Path parameters
fileId string The ID for the file.
permissionId string The ID for the permission.
Optional query parameters
transferOwnership boolean Whether changing a role to 'owner' should also downgrade the current owners to writers. (Default: false)

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file

Request body

In the request body, supply a Permissions resource.

Response

If successful, this method returns a Permissions resource in the response body.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library

import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.Permission;

import java.io.IOException;
// ...

public class MyClass {

  // ...

  /**
   * Patch a permission's role.
   *
   * @param service Drive API service instance.
   * @param fileId ID of the file to update permission for.
   * @param permissionId ID of the permission to patch.
   * @param newRole The value "owner", "writer" or "reader".
   * @return The patched permission if successful, {@code null} otherwise.
   */
  private static Permission patchPermission(Drive service, String fileId,
      String permissionId, String newRole) {
    Permission patchedPermission = new Permission();
    patchedPermission.setRole(newRole);
    try {
      return service.permissions().patch(
          fileId, permissionId, patchedPermission).execute();
    } catch (IOException e) {
      System.out.println("An error occurred: " + e);
    }
    return null;
  }

  // ...

}

.NET

Uses the .NET client library

using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;

using System.Net;
// ...

public class MyClass {

  // ...

  /// <summary>
  /// Patch a permission's role.
  /// </summary>
  /// <param name="service">Drive API service instance.</param>
  /// <param name="fileId">ID of the file to update permission for.</param>
  /// <param name="permissionId">ID of the permission to patch.</param>
  /// <param name="newRole">The value "owner", "writer" or "reader".</param>
  /// <returns>The patched permission, null is returned if an API error occurred</returns>
  public static Permission PatchPermission(DriveService service, String fileId,
      String permissionId, String newRole) {
    Permission patchedPermission = new Permission();
    patchedPermission.Role = newRole;
    try {
      return service.Permissions.Patch(patchedPermission, fileId, permissionId).Fetch();
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
    }
    return null;
  }

  // ...

}

PHP

Uses the PHP client library

/**
 * Patch a permission's role.
 *
 * @param Google_DriveService $service Drive API service instance.
 * @param String $fileId ID of the file to update permission for.
 * @param String $permissionId ID of the permission to patch.
 * @param String $newRole The value "owner", "writer" or "reader".
 * @return Google_Permission The patched permission. NULL is returned if an API error occurred.
 */
function patchPermission($service, $fileId, $permissionId, $newRole) {
  $patchedPermission = new Google_Permission();
  $patchedPermission->setRole($newRole);
  try {
    return $service->permissions->patch($fileId, $permissionId, $patchedPermission);
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
  return NULL;
}

Python

Uses the Python client library

from apiclient import errors
# ...

def patch_permission(service, file_id, permission_id, new_role):
  """Patch a permission's role.

  Args:
    service: Drive API service instance.
    file_id: ID of the file to update permission for.
    permission_id: ID of the permission to patch.
    new_role: The value 'owner', 'writer' or 'reader'.

  Returns:
    The patched permission if successful, None otherwise.
  """
  patched_permission = {'role': new_role}
  try:
    return service.permissions().patch(
        fileId=file_id, permissionId=permission_id,
        body=patched_permissions).execute()
  except errors.HttpError, error:
    print 'An error occurred: %s' % error
  return None

Ruby

Uses the Ruby client library

##
# Patch a permission's role
#
# @param [Google::APIClient] client
#   Authorized client instance
# @param [String] file_id
#   ID of the file to update permission for
# @param [String] permission_id
#   ID of the permission to patch
# @param [String] new_role
#   The value 'owner', 'writer' or 'reader'
# @return [Google::APIClient::Schema::Drive::V2::Permission]
#   The patched permission if successful, nil otherwise
def patch_permission(client, file_id, permission_id, new_role)

  drive = client.discovered_api('drive', 'v2')
  patched_permission = drive.permissions.patch.request_schema.new({
    'role' => new_role
  })
  result = client.execute(
    :api_method => drive.permissions.patch,
    :body_object => patched_permission,
    :parameters => {
      'fileId' => file_id,
      'permissionId' => permission_id
    })
  if result.status == 200
    return result.data
  else
    puts "An error occurred: #{result.data['error']['message']}"
  end
end

JavaScript

Uses the JavaScript client library

/**
 * Patch a permission's role.
 *
 * @param {String} fileId ID of the file to update permission for.
 * @param {String} permissionId ID of the permission to patch.
 * @param {String} newRole The value "owner", "writer" or "reader".
 */
function patchPermission(fileId, permissionId, newRole) {
  var body = {'role': newRole};
  var request = gapi.client.drive.permissions.patch({
    'fileId': fileId,
    'permissionId': permissionId,
    'resource': body
  });
  request.execute(function(resp) {
    console.log('New Role: ' + resp.role);
  });
}

Go

Uses the Go client library

import (
  "code.google.com/p/google-api-go-client/drive/v2"
  "fmt"
)

// PatchPermission patches a permission to the given role
func PatchPermission(d *drive.Service, fileId string, permissionId string,
    role string) error {
  p := &drive.Permission{Role: role}
  _, err := d.Permissions.Patch(fileId, permissionId, p).Do()
  if err != nil {
    fmt.Printf("An error occurred: %v\n", err)
    return err
  }
  return nil
}

Objective-C

Uses the Objective-C client library

#import "GTLDrive.h"
// ...

+ (void)patchPermissionWithService:(GTLServiceDrive *)service
                            fileId:(NSString *)fileId
                      permissionId:(NSString *)permissionId
                           newRole:(NSString *)newRole
                   completionBlock:(void (^)(GTLDrivePermission *, NSError *))completionBlock {
  GTLDrivePermission *patchedPermission = [GTLDrivePermission object];
  // The value @"owner", @"writer" or @"reader".
  patchedPermission.role = newRole;

  GTLQueryDrive *query =
    [GTLQueryDrive queryForPermissionsPatchWithObject:patchedPermission
                                               fileId:fileId
                                         permissionId:permissionId];
  // queryTicket can be used to track the status of the request.
  GTLServiceTicket *queryTicket =
    [service executeQuery:query
        completionHandler:^(GTLServiceTicket *ticket,
                            GTLDrivePermission *permission, NSError *error) {
          if (error == nil) {
            completionBlock(permission, nil);
          } else {
            NSLog(@"An error occurred: %@", error);
            completionBlock(nil, error);
          }
        }];
}

// ...

Try it!

Use the APIs Explorer below to call this method on live data and see the response.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.