2

我嘗試上傳文件並將其共享給公衆。文件已上傳,但我無法設置權限。這裏是我的代碼:驅動器SDK更改權限需要登錄

<?php 
.... 
$client = new Google_Client(); 
$client->setClientId(GOOGLE_ID); 
$client->setClientSecret(GOOGLE_SECRET); 
$client->setAccessType("offline"); 
$client->setScopes(array(//just to be sure included all scopes 
    "https://www.googleapis.com/auth/drive", 
    "https://www.googleapis.com/auth/drive.file", 
    "https://www.googleapis.com/auth/drive.readonly.metadata", 
    "https://docs.google.com/feeds", 
    "https://www.googleapis.com/auth/drive.apps.readonly", 
    "https://www.googleapis.com/auth/drive.readonly", 
    "https://www.googleapis.com/auth/drive.install", 
    "https://www.googleapis.com/auth/drive.appdata", 
    "https://www.googleapis.com/auth/drive.scripts" 
)); 
$service = new Google_Service_Drive($client); 
... 
//setting a token and successfully uploading a file 
... 
$perm = new Google_Service_Drive_Permission(); 
$perm->setType("anyone"); 
$perm->setRole("reader"); 
$perm->setValue(""); 
$perm->setWithLink(false); 
try { 
    $a = $service->permissions->insert($file->getId(), $perm); 
} catch (Exception $e) { 
    print "An error occurred: " . $e->getMessage(); 
} 

它不會拋出任何異常,返回一個對象。從這個文件中獲得

順便說一句,這些權限:

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "required", 
    "message": "Login Required", 
    "locationType": "header", 
    "location": "Authorization" 
    } 
    ], 
    "code": 401, 
    "message": "Login Required" 
} 
} 

編輯 我在任一值或權限的ID必須發送文檔中。如果我得到這個權利,比ID必須是我的應用程序電子郵件?我試圖$perm->setId(GOOGLE_ID);但沒有來得及換上

響應轉儲:

Google_Http_Request Object 
(
[batchHeaders:Google_Http_Request:private] => Array 
     (
      [Content-Type] => application/http 
      [Content-Transfer-Encoding] => binary 
      [MIME-Version] => 1.0 
     ) 

    [queryParams:protected] => Array 
     (
     ) 

    [requestMethod:protected] => POST 
    [requestHeaders:protected] => Array 
     (
      [content-type] => application/json; charset=UTF-8 
      [authorization] => Bearer xxxxxxxxxxxxxx 
     ) 

    [baseComponent:protected] => https://www.googleapis.com 
    [path:protected] => /drive/v2/files/0B3_LVXxRuaPoT0xQYUZxbjlQTUE/permissions 
    [postBody:protected] => {"id":"[email protected]account.com","role":"reader","type":"anyone"} //its a test value, i send my id instead of me 
    [userAgent:protected] => 
    [canGzip:protected] => 
    [responseHttpCode:protected] => 
    [responseHeaders:protected] => 
    [responseBody:protected] => 
    [expectedClass:protected] => Google_Service_Drive_Permission 
    [accessKey] => 
    ) 

編輯 我還試圖用$permissionId = $service->permissions->getIdForEmail($email);方法,但它並沒有太多的工作,似乎沒有什麼工作是相關聯具有權限。

+0

是你在'$ a'結果中顯示的json代碼? – Robbert

+0

@Robbert不,它顯示在$ a userPermission => selfLink,https://www.googleapis.com/drive/v2/files/0BzWQyyYOE3TpV3VYT0thT2lsM1U/permissions/me檢索的鏈接中,但我不知道它是否可以工作爲你 – user3325976

回答

1

根據這裏的答案,How to create a public Google Doc through the Drive API (PHP client),你應該通過'我'設置值。

$permission = new Google_Permission(); 
$permission->setRole('writer'); 
$permission->setType('anyone'); 
$permission->setValue('me'); 
$service->permissions->insert($file->getId(), $permission); 

答案使用writer的權限級別,但reader應該正常工作。

我看到你的代碼和引用的答案之間的其他區別。答案用戶Google_DriveService當你使用Google_Service_Drive

+0

仍在測試,但文檔說明讀者級別不要求價值 – user3325976

+0

不,它沒有幫助,有什麼想法? @Robbert – user3325976

+0

與我應該使用客戶端ID:應用程序ID或應用程序電子郵件?有關係嗎? – user3325976