2016-10-27 46 views
0

我想開發一個應用程序,其中我使用谷歌驅動器上傳和共享文件在service。但是,問題是共享鏈接有private訪問權限。我想改變對anyone with link的訪問權限。爲了實現我使用Google Drive REST API,但是當我執行代碼時似乎沒有發生。這裏是我的代碼:連接到谷歌API後臺服務的Java客戶端

public void changePermissionSettings(String resourceId) throws GeneralSecurityException, IOException, URISyntaxException { 

     com.google.api.services.drive.Drive driveService = getDriveService(); 

     JsonBatchCallback<Permission> callback = new JsonBatchCallback<com.google.api.services.drive.model.Permission>() { 
      @Override 
      public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException { 
       Log.e("upload", "Permission Setting failed"); 
      } 

      @Override 
      public void onSuccess(com.google.api.services.drive.model.Permission permission, HttpHeaders responseHeaders) throws IOException { 
       Log.e("upload", "Permission Setting success"); 
      } 
     }; 

     final BatchRequest batchRequest = driveService.batch(); 

     com.google.api.services.drive.model.Permission contactPermission = new com.google.api.services.drive.model.Permission() 
       .setType("anyone") 
       .setRole("reader"); 
     driveService.permissions().create(resourceId, contactPermission) 
       .setFields("id") 
       .queue(batchRequest, callback); 

     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        batchRequest.execute(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }).start(); 
    } 


    private com.google.api.services.drive.Drive getDriveService() throws GeneralSecurityException, 
      IOException, URISyntaxException { 

     Collection<String> elenco = new ArrayList<>(); 
     elenco.add("https://www.googleapis.com/auth/drive"); 

     GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(this, elenco) 
       .setSelectedAccountName(getAccountName()); 

     return new com.google.api.services.drive.Drive.Builder(
       AndroidHttp.newCompatibleTransport(), new JacksonFactory(), credential).build(); 
    } 

我有兩個問題在這裏:

  1. 那麼,是否可以同時使用谷歌雲端硬盤Android API和谷歌驅動器REST API一起?

  2. 如何將文件的權限設置從private更改爲anyone with linkDocumentation提到Authorization happens in response to receiving an error when sending a request.如何才能實現service ?? 「

回答

0

」是否同時使用Google Drive Android API和Google Drive REST API?「

如所述here,這是可能的。 「只要看看這個Lifecycle of a Drive file,就可以將您的應用想象成最上方的圖片(Android應用),底部的REST API(Drive Service)。」

有幾點要記住,雖然:

  1. 的GDAA的主要標識,驅動器Id住在GDAA(GooPlaySvcs)僅在REST API不存在。您必須檢索「ResourceId」,它是REST Api中的主要標識符(請參閱SO 29030110)。

  2. RESOURCEID可以從驅動器Id後才GDAA提交(上傳)的文件/文件夾中獲得(見SO 22874657

  3. 你會碰到很多造成的事實,時間問題是GDAA「緩衝區」網絡請求在它自己的時間表(系統優化),而REST Api讓你的應用程序控制等待響應。一般來說,如果你掃描這些SO questions,你會發現很多關於這些問題的喋喋不休(儘管如此)。

我保持兩個GDAA最小的CRUD包裝和REST API,可以幫助你,如果你把它們合併(以兩者在MainActivity幾乎是相同和CRUD方法具有相同的簽名)。

「如何改變從private文件的權限設置,以anyone with linkDocumentation提到Authorization happens in response to receiving an error when sending a request.那怎麼可以在服務實現?「

從這個thread基礎,你必須設置以下permissions

private Permission insertPermission(Drive service, String fileId) throws Exception{ 
    Permission newPermission = new Permission(); 
    newPermission.setType("anyone"); 
    newPermission.setRole("reader"); 
    newPermission.setValue(""); 
    newPermission.setWithLink(true); 
    return service.permissions().insert(fileId, newPermission).execute(); 
} 

您還可以檢查此鏈接:。Google Drive SDK - change item sharing permissions

相關問題