2013-08-28 32 views
2

使用谷歌驱动器SDK API的文件列表,我不能修复...下面你可以找到代码:显示在Java中

import java.io.IOException; 
import java.net.URISyntaxException; 
import java.security.GeneralSecurityException; 

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; 
import com.google.api.client.http.HttpTransport; 
import com.google.api.client.http.javanet.NetHttpTransport; 
import com.google.api.client.json.JsonFactory; 
import com.google.api.client.json.jackson.JacksonFactory; 
import com.google.api.services.drive.Drive; 
import com.google.api.services.drive.DriveScopes; 
import com.google.api.services.drive.Drive.Files; 
import com.google.api.services.drive.model.File; 
import com.google.api.services.drive.model.FileList; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.List; 

public class getDriveService { 


    /** Email of the Service Account */ 
    private static final String SERVICE_ACCOUNT_EMAIL = "<email_address_service>@developer.gserviceaccount.com"; 

    /** Path to the Service Account's Private Key file */ 
    private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "src/<file_key>-privatekey.p12"; 

    public static void main(String[] args) throws IOException, GeneralSecurityException, URISyntaxException { 

     List<File> lista = null; 

     lista = retrieveAllFiles(getDriveService()); 

     System.out.println(""+lista.size()); 
     for (File file : lista) { 
      System.out.println(""+file.getId()); 
     } 
    } 

    /** 
    * Build and returns a Drive service object authorized with the service accounts 
    * that act on behalf of the given user. 
    * 
    * @param userEmail The email of the user. 
    * @return Drive service object that is ready to make requests. 
    */ 
    public static Drive getDriveService() throws GeneralSecurityException, 
     IOException, URISyntaxException { 

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

     HttpTransport httpTransport = new NetHttpTransport(); 
     JacksonFactory jsonFactory = new JacksonFactory(); 
     GoogleCredential credential = new GoogleCredential.Builder() 
       .setTransport(httpTransport) 
       .setJsonFactory(jsonFactory) 
       .setServiceAccountId(SERVICE_ACCOUNT_EMAIL) 
       .setServiceAccountScopes(elenco) 
       .setServiceAccountPrivateKeyFromP12File(
         new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH)) 
       .build(); 
     Drive service = new Drive.Builder(httpTransport, jsonFactory, null) 
       .setApplicationName("FileListAccessProject") 
       .setHttpRequestInitializer(credential).build(); 

     return service; 
} 

    /** 
     * Retrieve a list of File resources. 
     * 
     * @param service Drive API service instance. 
     * @return List of File resources. 
     */ 
     private static List<File> retrieveAllFiles(Drive service) throws IOException { 
     List<File> result = new ArrayList<File>(); 
     Files.List request = service.files().list(); 

     do { 
      try { 
      FileList files = request.execute(); 

      result.addAll(files.getItems()); 
      request.setPageToken(files.getNextPageToken()); 
      } catch (IOException e) { 
      System.out.println("An error occurred: " + e); 
      request.setPageToken(null); 
      } 
     } while (request.getPageToken() != null && 
       request.getPageToken().length() > 0); 

     return result; 
     } 


} 

如果我经营的主要方法,它显示为“0”。文件列表是空的。为什么?

该代码似乎是正确的,我也从谷歌API控制台面板启用服务“驱动器API”...我离开禁用“驱动器SDK”(我不明白为什么,但这里https://developers.google.com/drive/delegation这么说)

什么可能是错的?我忘了什么吗?

预先感谢任何帮助

弗朗西斯

回答

0

您必须设置ServiceAccountUser为用户的驱动器,你想访问,并ServiceAccountId作为服务帐户,用户

是这样的:

GoogleCredential credentialUser = new GoogleCredential.Builder().setTransport(httpTransport) 
.setJsonFactory(JSON_FACTORY) 
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL) 
.setServiceAccountUser("[email protected]") 
.setServiceAccountPrivateKeyFromP12File(new File(SERVICE_ACCOUNT_PKCS12_FILE_PATH)) 
.setServiceAccountScopes(serviceAccountScopes).build();