2016-09-07 36 views
0

我已從google documentation採取以下代碼。Google Drive API中setFields的有效值

public static void detectDriveChanges() throws IOException { 

    StartPageToken response = DRIVE.changes() 
      .getStartPageToken().execute(); 

    String savedStartPageToken = response.getStartPageToken(); 
    System.out.println("Start token: " + savedStartPageToken); 

    // Begin with our last saved start token for this user or the 
    // current token from getStartPageToken() 
    String pageToken = savedStartPageToken; 
    while (pageToken != null) { 
     ChangeList changes = DRIVE.changes().list(pageToken) 
       .setFields("*") 
       .execute(); 
     for (Change change : changes.getChanges()) { 
      // Process change 
      System.out.println("Change found for file: " + change.getFileId()); 
     } 
     if (changes.getNewStartPageToken() != null) { 
      // Last page, save this token for the next polling interval 
      savedStartPageToken = changes.getNewStartPageToken(); 


     } 
     pageToken = changes.getNextPageToken(); 
    } 
} 

.setFields( 「*」)導致以下爲請求響應。

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request 
{ 
    "code" : 400, 
    "errors" : [ { 
    "domain" : "global", 
    "message" : "Bad Request", 
    "reason" : "badRequest" 
    } ], 
    "message" : "Bad Request" 

如果我將setfields中的*改爲text,那麼我得到的參數無效。如果我完全刪除它,我不會有任何錯誤。我試圖確定在這種情況下setFields的可能參數是什麼,但是我沒有找到任何東西。

在這種情況下,我可以在哪裏找到setFields的可能參數列表?

爲什麼當setFields設置爲*

我使用以下依賴

<dependency> 
    <groupId>com.google.apis</groupId> 
    <artifactId>google-api-services-drive</artifactId> 
    <version>v3-rev40-1.22.0</version> 
</dependency> 

問候 康特

+0

我該在哪裏回答? – xenteros

回答

2

setField爲驅動器API上面的代碼不能用於局部響應,它將取決於你想要哪些數據將成爲返回對象的一部分。

設置「*」將不起作用,因爲它不代表Response對象中的任何字段。對於它的工作,你要麼不設置字段,以獲取所有值,或指定僅需要(取決於你打電話,對變更列表API的領域,它可以changesnextPageTokennewStartPageTokenkind

0

我問的問題中的代碼需要被分成兩個函數,如下所示,作爲SAVED_START_PAGE_TOKEN需要首先設置,然後可以列出對驅動器所做的任何後續更改。我發佈了這個文件,以便清楚說明。

/** 
* Sets SAVED_START_PAGE_TOKEN. Now any changes in google drive 
* the occur after this point can be listed in the the function 
* detectDriveChanges 
* @throws IOException 
*/ 
public static void SetStartPageToken() throws IOException { 
    StartPageToken response = DRIVE.changes().getStartPageToken().execute(); 
    SAVED_START_PAGE_TOKEN = response.getStartPageToken(); 
    System.out.println("Start token: " + SAVED_START_PAGE_TOKEN); 
} 

/** 
* List any changes to the google drive since the last time 
* SAVED_START_PAGE_TOKEN was set 
* @throws IOException 
*/ 
public static void detectDriveChanges() throws IOException { 
    // Begin with our last saved start token for this user or the 
    // current token from getStartPageToken() 
    String pageToken = SAVED_START_PAGE_TOKEN; 
    while (pageToken != null) { 
     ChangeList changes = DRIVE.changes().list(pageToken) 
       .setFields("changes") 
       .execute(); 
     for (Change change : changes.getChanges()) { 
      System.out.println("Change found for file: " + change.getFileId()); 
     } 
     if (changes.getNewStartPageToken() != null) { 
      // Last page, save this token for the next polling interval 
      SAVED_START_PAGE_TOKEN = changes.getNewStartPageToken(); 
     } 
     pageToken = changes.getNextPageToken(); 
    } 
} 
0

刪除.setFilters的作品,但我仍然想要減輕流量和內存使用量。 此列表幫助我fin d MIME類型的字段名稱,它原來是'mimeType'區分大小寫的! try fields listed here

我需要整理所有文件中的文件夾,導致文件夾也是谷歌驅動器上的文件。 這是我所需要的東西:

.setFields(的 「nextPageToken,文件(ID,姓名,mime類型)」)

好運。