Google app engine 从Google App Engine(Java版本)将数据导出到Google电子表格
我现在不知道去哪里了。当我搜索“从Google App Engine将数据导出到Google电子表格”的示例时,我看到了很多Google转换API、Google转换API、Google电子表格API和Google Docs API教程。但当我在谷歌网站上查看时,他们都不赞成谷歌的做法???那么,什么是最新的,现在我可以利用它 ===== 好的,现在我通过OAuth2使用Google Drive SDK来创建文本文件。但我有一个问题: 这就是错误: java.lang.NullPointerExceptionGoogle app engine 从Google App Engine(Java版本)将数据导出到Google电子表格,google-app-engine,google-sheets,google-drive-api,google-spreadsheet-api,Google App Engine,Google Sheets,Google Drive Api,Google Spreadsheet Api,我现在不知道去哪里了。当我搜索“从Google App Engine将数据导出到Google电子表格”的示例时,我看到了很多Google转换API、Google转换API、Google电子表格API和Google Docs API教程。但当我在谷歌网站上查看时,他们都不赞成谷歌的做法???那么,什么是最新的,现在我可以利用它 ===== 好的,现在我通过OAuth2使用Google Drive SDK来创建文本文件。但我有一个问题: 这就是错误: java.lang.NullPointerExc
位于 java.net.URI$Parser.parse(URI.java:3004)
java.net.URI.(URI.java:577)
com.google.api.client.http.GenericUrl.(GenericUrl.java:100)
com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:269)
在 com.google.api.services.drive.drive$Files$Insert.executeUnparsed(drive.java:309)
在 com.google.api.services.drive.drive$Files$Insert.execute(drive.java:331)
在 com.company.dashboard.service.DriveService.initDoc(DriveService.java:84) 代码如下:
private GoogleCredential buildGoogleCredential(Credential credential) {
try {
logger.warning(oauth2Service.getClientCredential().toString());
GoogleCredential googleCredential = new GoogleCredential.Builder()
.setClientSecrets(oauth2Service.getClientCredential())
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory()).build();
googleCredential.setAccessToken(credential.getAccessToken());
googleCredential.setRefreshToken(credential.getRefreshToken());
return googleCredential;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private Drive buildService(GoogleCredential credential) {
return new Drive.Builder(new NetHttpTransport(), new JacksonFactory(), credential).build();
}
public void initDoc(HttpServletRequest req)
throws Exception {
User user = UserServiceFactory.getUserService().getCurrentUser();
Credential credential = oauth2Service.getStoredCredential(
user.getUserId(),
(CredentialStore)req.getSession().getServletContext().getAttribute(OAuth2Constant.GOOG_CREDENTIAL_STORE));
if (credential != null) {
logger.warning("Using access token: " + credential.getAccessToken());
try {
GoogleCredential googleCredential = buildGoogleCredential(credential);
Drive service = buildService(googleCredential);
if (service == null) {
logger.warning("very bad!");
}
File body = new File();
body.setTitle("My document");
body.setDescription("A test document");
body.setMimeType("text/plain");
java.io.File fileContent = new java.io.File("document.txt");
FileContent mediaContent = new FileContent("text/plain", fileContent);
service.files().insert(body, mediaContent).execute();
//File file = service.files().insert(body, mediaContent).execute();
//System.out.println("File ID: " + file.getId());
} catch (HttpResponseException e) {
if (e.getStatusCode() == 401) {
logger.warning(e.getMessage());
// Credentials have been revoked.
// TODO: Redirect the user to the authorization URL.
throw new UnsupportedOperationException();
}
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
}
oauth2Service.getClientCredential()返回(xxx=客户端id和客户端密码,此处未显示)
{“web”:{“client_id”:“xxx.apps.googleusercontent.com”,“client_secret”:“xxx”}
这是我的工作范围:
final static List<String> SCOPES = Arrays.asList("https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/docs",
"https://www.googleapis.com/auth/drive.file");
final static String AUTH_RESOURCE_LOC = "/client_secrets.json";
final static String OATH_CALLBACK = "http://localhost:8888/oauth2callback";
final static List SCOPES=Arrays.asList(“https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/docs",
"https://www.googleapis.com/auth/drive.file");
最终静态字符串AUTH_RESOURCE_LOC=“/client_secrets.json”;
最终静态字符串\u回调=”http://localhost:8888/oauth2callback";
这条线
service.files().insert(body,mediaContent.execute()
抛出NullPointerException。知道哪里出了问题吗
p/S:Credential=com.google.api.client.auth.oauth2.Credential。我有我的OAuth2一切都很好。我可以毫无问题地检索用户信息,但无法检索驱动器API。服务不是空的,正如你所看到的,我把它放在“非常糟糕”的日志中,它没有显示出来。没有引发401异常意味着我的Oauth2对作用域很好
=======
该死!!!!终于解决了问题!!!我的代码完全正确!只是我启用了错误的API!它应该是驱动器API而不是驱动器SDK API://您可以从GAE生成一个csv文件,并使用驱动器API和
?convert=true
上传该文件,以便将其自动转换为Google电子表格:
从何处导出数据?数据存储?我想从GAE生成用于报告的电子表格。我想要这个解决方案。@danholevet,到谷歌硬盘:)FileWriter在GAE中不受支持@@“哈哈!我已经做到了:)使用这个magic=>”服务.files().insert(file,ByteArrayContent.fromString(clientFile.mimeType,clientFile.content)).setConvert(true.execute();"