2012-02-29 48 views
6

使用Google Docs Java API和Google Apps帳戶,是否可以模擬用戶並下載文件?Google Docs API - 模擬用戶文件下載

當我運行下面的程序時,它清楚地登錄到域和模擬用戶,因爲它檢索其中一個文件的詳細信息並打印出文件標題。但是,當它嘗試下載文件時,會引發ServiceForbiddenException。

如果Java API無法實現,是否有人知道我的程序是否可以使用協議API編寫HTTP請求來下載文件?

public class AuthExample { 

private static DocsService docService = new DocsService("Auth Example"); 

public static void main(String[] args) 
    throws Exception 
{ 
    String adminUser = args[0]; 
    String adminPassword = args[1]; 
    String authToken = args[2]; 
    String impersonatedUser = args[3]; 

    loginToDomain(adminUser, adminPassword, authToken); 

    URL url = new URL("https://docs.google.com/feeds/" + impersonatedUser + "/private/full"); 
    DocumentListFeed feed = docService.getFeed(url, DocumentListFeed.class); 

    DocumentListEntry entry = feed.getEntries().get(0); 

    String title = entry.getTitle().getPlainText(); 
    System.out.println(title); 

    String type = entry.getType(); 
    if (type.equals("document")) 
    { 
     String encodedAdminUser = URLEncoder.encode(adminUser); 
     String resourceId = entry.getResourceId(); 
     String resourceIdNoPrefix = resourceId.substring(resourceId.indexOf(':')+1); 

     String downloadUrl = 
       "https://docs.google.com/feeds/download/documents/Export" + 
       "?xoauth_requestor=" + encodedAdminUser + 
       "&docId=" + resourceIdNoPrefix + 
       "&exportFormat=doc"; 

     downloadFile(downloadUrl, title + ".doc"); 
    } 
} 

private static void loginToDomain(String adminUser, String adminPassword, String authToken) 
     throws OAuthException, AuthenticationException 
{ 
    String domain = adminUser.substring(adminUser.indexOf('@')+1); 

    GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); 
    oauthParameters.setOAuthConsumerKey(domain); 
    oauthParameters.setOAuthConsumerSecret(authToken); 
    oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH); 
    oauthParameters.setScope("https://docs.google.com/feeds/ http://spreadsheets.google.com/feeds/ http://docs.googleusercontent.com/"); 

    docService.useSsl(); 
    docService.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer()); 
    docService.setUserCredentials(adminUser, adminPassword); 
} 


// Method pasted directly from Google documentation 
public static void downloadFile(String exportUrl, String filepath) 
     throws IOException, MalformedURLException, ServiceException 
{ 
    System.out.println("Exporting document from: " + exportUrl); 

    MediaContent mc = new MediaContent(); 
    mc.setUri(exportUrl); 
    MediaSource ms = docService.getMedia(mc); 

    InputStream inStream = null; 
    FileOutputStream outStream = null; 

    try { 
     inStream = ms.getInputStream(); 
     outStream = new FileOutputStream(filepath); 

     int c; 
     while ((c = inStream.read()) != -1) { 
      outStream.write(c); 
     } 
    } finally { 
     if (inStream != null) { 
      inStream.close(); 
     } 
     if (outStream != null) { 
      outStream.flush(); 
      outStream.close(); 
     } 
    } 
} 

}

+0

我在完全相同的問題上運行。有沒有人有關於這個問題的信息? – Jerome 2013-01-16 10:48:07

回答

4

模擬將工作打算,如果你使用的oauth2與ServiceAccounts