从Google App Engine将数据导出到Google电子表格(Java版)

时间:2012-11-28 03:01:42

标签: google-app-engine google-sheets google-drive-api google-spreadsheet-api

我现在没有。当我谷歌找到“从Google App Engine将数据导出到Google电子表格”时,我看到了很多Google Conversion API,Google Conversion API,Google Spreadsheet API和Google Docs API教程。但是,当我在Google网站上查看时,他们都被Google弃用了?那么,现在最新的是什么,所以我可以利用它?

=====

好的,现在我通过OAuth2使用Google Drive SDK来创建文本文件。但我对此有疑问:

这是错误:

  

java.lang.NullPointerException
at   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 $文件$ Insert.executeUnparsed(Drive.java:309)
    在   com.google.api.services.drive.Drive $文件$ 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和客户端密码,此处未显示)

  

{ “网络”:{ “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";

这一行

  

service.files()。insert(body,mediaContent).execute();

抛出NullPointerException。知道出了什么问题???

P / S:Credential = com.google.api.client.auth.oauth2.Credential。我有我的OAuth2一切正常。我可以毫无问题地检索用户信息,但不能检索Drive API。服务不是null,因为你看到我把它作为“非常糟糕”的日志,它没有显示。没有抛出401异常意味着我的Oauth2对范围很好。

=======

DARN !!!!终于解决了问题!我的代码完全正确!我启用了错误的API!它应该是Drive API而不是Drive SDK API:/

1 个答案:

答案 0 :(得分:0)

您可以从GAE生成csv文件,并使用带有?convert=true的云端硬盘API将其上传,以便将其自动转换为Google电子表格:

https://developers.google.com/drive/v2/reference/files/insert