阅读Java中的Google电子表格

时间:2016-09-08 20:34:35

标签: java api spreadsheet

所以,我已经从摆弄东西中学到了我所有的Java知识。话虽如此,除了保存和加载之外,我还有一个工作应用程序。

我想从Google电子表格中保存并加载。

我查找了Google API文档,并在我的项目中安装了API ...我也复制了一些我看过的例子。 (What is an example of using OAuth 2.0 and Google Spreadsheet API with Java?

我想知道下一步是什么。现在我将它重定向到谷歌,因为我不知道在哪里重定向到它(它是一个桌面应用程序,而不是一个网络应用程序),我不明白如何使用附加到google.com网址。

是的,我知道这看起来像我只是复制和粘贴,但实际上这是该计划工作的唯一障碍,所以我很感激一些帮助:/

谢谢!

package com.thepanthurr.security;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.*;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;

import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

public class InputOutput {
    public static final String USERNAME = "Redacted.apps.googleusercontent.com";
    public static final String PASSWORD = "Redacted";
    public static final String SHEETURL = "https://www.google.com/";

    private static final List<String> SCOPES = Arrays.asList("https://www.googleapis.com/auth/spreadsheets");

    public static void read() throws IOException, URISyntaxException {
        Credential credential = getCredentials();
        System.out.println(credential.toString());
    }

    public static Credential getCredentials() throws IOException, URISyntaxException {
        HttpTransport transport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();

        String authorizationUrl =
                new GoogleAuthorizationCodeRequestUrl(USERNAME, SHEETURL, SCOPES).build();

        Desktop.getDesktop().browse(new URL(authorizationUrl).toURI());

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String code = in.readLine(); //NO CLUE WHAT TO PUT HERE

        GoogleTokenResponse response =
                new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, USERNAME, PASSWORD,
                        code, SHEETURL).execute();

        return new GoogleCredential.Builder().setClientSecrets(USERNAME, PASSWORD)
                .setJsonFactory(jsonFactory).setTransport(transport).build()
                .setAccessToken(response.getAccessToken()).setRefreshToken(response.getRefreshToken());
        //What do I do after I receive the credentials?
    }
}

0 个答案:

没有答案