2012-02-29 14 views
6

Google Apps खाते के साथ Google डॉक्स जावा एपीआई का उपयोग करना, क्या उपयोगकर्ता को प्रतिरूपण करना और फ़ाइल डाउनलोड करना संभव है?Google डॉक्स एपीआई - उपयोगकर्ता फ़ाइल का प्रतिरूपण डाउनलोड करें

जब मैं नीचे दिया गया प्रोग्राम चलाता हूं, तो यह स्पष्ट रूप से डोमेन पर लॉग ऑन कर रहा है और उपयोगकर्ता का प्रतिरूपण कर रहा है क्योंकि यह फ़ाइलों में से किसी एक का विवरण पुनर्प्राप्त करता है और फ़ाइल शीर्षक को प्रिंट करता है। हालांकि, जब यह फ़ाइल डाउनलोड करने का प्रयास करता है, तो एक सेवाफोरबिडन अपवाद फेंक दिया जाता है।

यदि जावा एपीआई के साथ यह संभव नहीं है, तो क्या किसी को पता है कि मेरे प्रोग्राम के लिए प्रोटोकॉल एपीआई का उपयोग कर फ़ाइल डाउनलोड करने के लिए 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

मैं ठीक उसी समस्या में चलाने के साथ OAuth2 का उपयोग काम करेंगे। क्या किसी को उस समस्या के बारे में जानकारी है? – Jerome

उत्तर

4

प्रतिरूपण का इरादा के रूप में यदि आप ServiceAccounts