0

我现在无处可去。当我在 Google 上四处寻找“从 Google App Engine 将数据导出到 Google 电子表格”的示例时,我看到了很多 Google Conversion API、Google Conversion API、Google Spreadsheet API 和 Google Docs API 教程。但是当我在谷歌网站上查看时,它们都被谷歌弃用了???那么,现在最新的更新是什么,以便我可以使用它?

=====

好的,现在我通过 OAuth2 使用 Google Drive SDK 创建文本文件。但我有这个问题:

这是错误:

java.lang.NullPointerException
at java.net.URI$Parser.parse(URI.java:3004)
at java.net.URI.(URI.java:577)
at 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";

这条线

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

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

P/S:凭据 = com.google.api.client.auth.oauth2.Credential。我的 OAuth2 一切正常。我可以毫无问题地检索用户信息,但不能检索 Drive API。如您所见,服务不为空,我将其记录为“非常糟糕”并且未显示。没有抛出 401 异常意味着我的 Oauth2 对范围很好。

=======

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

4

1 回答 1

0

您可以从 GAE 生成一个 csv 文件并使用 Drive API 将其上传,?convert=true以使其自动转换为 Google 电子表格:

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

于 2012-11-28T06:07:32.287 回答