Silko
Silko

Reputation: 602

Google Drive REST API - Invalid permission value for only one user

I implemented Google Drive file permissions into my application. Everything works perfectly, only for one user (email), when I try to insert a permission, I get

bad request (400): Invalid permission value

Anyone has an idea why would I get this error just for one user? I don't see any restrictions for this user, but maybe I missed something.

Here is my insert permission function (I am using google api client (REST API v2)):

public function insert_permission($FileID, $Value, $Type, $Role, $OptParams = array(), $Service = NULL) {
    if(empty($Service)) {
       $Client = $this->get_client_sa();
       $Service = new Google_Service_Drive($Client);
    }

    $NewPermission = new Google_Service_Drive_Permission();
    $NewPermission->setValue($Value);
    //$NewPermission->setEmailAddress($Value);
    $NewPermission->setType($Type);
   // commenter additional role
   if($Role == "commenter") {
       $Role = "reader";
       $NewPermission->setAdditionalRoles(["commenter"]);
   }
    $NewPermission->setRole($Role);

    $i = 0;
    $maxTries = 3;
    $ErrorMessage = "";
    while($i < $maxTries) {
        try {
            return $Service->permissions->insert($FileID, $NewPermission, $OptParams);
        } catch (Exception $E) {
            $ErrorMessage = $E->getMessage();
            if($E->getCode() != 500)
                $i++;
        }
    }

    $this->session->set_userdata("error", $ErrorMessage);
    return $E;
}

While I was debugging I created var_dump($httpRequest); before calling $this->client->execute($httpRequest), and I got this object (I deleted user email, file id and authorization token from the object for security and privacy reasons):

object(Google_Http_Request)#69 (15) {
  ["batchHeaders":"Google_Http_Request":private]=>
  array(3) {
    ["Content-Type"]=>
    string(16) "application/http"
    ["Content-Transfer-Encoding"]=>
    string(6) "binary"
    ["MIME-Version"]=>
    string(3) "1.0"
  }
  ["queryParams":protected]=>
  array(0) {
  }
  ["requestMethod":protected]=>
  string(4) "POST"
  ["requestHeaders":protected]=>
  array(2) {
    ["content-type"]=>
    string(31) "application/json; charset=UTF-8"
    ["authorization"]=>
    string(183) "token_string"
  }
  ["baseComponent":protected]=>
  string(26) "https://www.googleapis.com"
  ["path":protected]=>
  string(72) "/drive/v2/files/file_id/permissions"
  ["postBody":protected]=>
  string(64) "{"role":"writer","type":"user","value":"user_email"}"
  ["userAgent":protected]=>
  NULL
  ["canGzip":protected]=>
  NULL
  ["responseHttpCode":protected]=>
  NULL
  ["responseHeaders":protected]=>
  NULL
  ["responseBody":protected]=>
  NULL
  ["expectedClass":protected]=>
  string(31) "Google_Service_Drive_Permission"
  ["expectedRaw":protected]=>
  bool(false)
  ["accessKey"]=>
  NULL
}

Everything looks OK to me and as I already said it works for all other users. I tested this call with Postman for the same user that is giving me problems and it worked, so there should be something wrong with my code.

Upvotes: 1

Views: 1298

Answers (1)

Silko
Silko

Reputation: 602

Now I am totally embarrassed. I was not validating user email input correctly so it had a space character at the end and that was the only problem.

Upvotes: 2

Related Questions