gpt4 book ai didi

google-docs - Google Docs API - 模拟用户文件下载

转载 作者:行者123 更新时间:2023-12-03 09:37:31 27 4
gpt4 key购买 nike

将 Google Docs Java API 与 Google Apps 帐户一起使用,是否可以模拟用户并下载文件?

当我运行下面的程序时,它显然是登录到域并冒充用户,因为它检索其中一个文件的详细信息并打印出文件标题。但是,当它尝试下载文件时,会引发 ServiceForbiddenException。

如果 Java API 不可能,有谁知道我的程序是否可以编写 HTTP 请求以使用协议(protocol) API 下载文件?

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();
}
}
}

}

最佳答案

如果您将 Oauth2 与 ServiceAccounts 一起使用,模拟将按预期工作

关于google-docs - Google Docs API - 模拟用户文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9503360/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com