Accessing Google Sheets from Spring MVC



我试图访问谷歌表从我的后端,写在Spring MVC。使用教程,我得到这样的内容:

public static Sheets getSheetsService() throws IOException {
    return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, authorize())
            .setApplicationName(APPLICATION_NAME)
            .build();
}
public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in =
            ConcreteSheetsController.class.getResourceAsStream("/secret.json");
    GoogleClientSecrets clientSecrets =
            GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                    .setDataStoreFactory(DATA_STORE_FACTORY)
                    .setAccessType("offline")
                    .build();
    Credential credential = new AuthorizationCodeInstalledApp(
            flow, new LocalServerReceiver()).authorize("user");
    System.out.println(
            "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}

secret的创建方式如下所示:https://developers.google.com/sheets/quickstart/java

这是"工作",但应用程序的输出为我提供了一个链接。当我点击它,然后我的浏览器要求我选择一个谷歌帐户。如果我这样做一切都很好,但我想有一个更合适的解决方案的服务器。我想知道我的秘密。json文件,使我的应用程序登录,而不需要我提供我自己的用户帐户。有什么办法能做到吗?

尝试使用Gdata第三方库而不是google.v4。下面是我用来传递凭证的代码。您必须在google的控制台创建一个P12文件。

那么很明显,你的大部分工作表/工作簿的读/写将不得不改变以匹配gdata方法。

我希望这对你有帮助,如果没有,请提出任何问题,我也许能帮上忙。

    // Application name
    private static final String APPLICATION_NAME = "spreadsheet-application-name";
    // account info and p12
    private static final String ACCOUNT_P12_ID = "P12@Account";
    private static File P12FILE;
    private static String ssName = "Spreadsheet name";
    private static String wsName = "worksheetname"; //this has to be all lower case for some reason
    // scopes
    private static final List<String> SCOPES = Arrays.asList(
            "https://docs.google.com/feeds",
            "https://spreadsheets.google.com/feeds");
    // Spreadsheet API URL
    private static final String SPREADSHEET_URL = "https://spreadsheets.google.com/feeds/spreadsheets/private/full";
    private static final URL SPREADSHEET_FEED_URL;
    static {
        try {
            SPREADSHEET_FEED_URL = new URL(SPREADSHEET_URL);
            P12FILE = getP12File();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }
    // Authorize-
    private static Credential authorize() throws Exception {
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = new JacksonFactory();
        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId(ACCOUNT_P12_ID)
                .setServiceAccountPrivateKeyFromP12File(P12FILE)
                .setServiceAccountScopes(SCOPES)
                .build();
//        boolean ret = credential.refreshToken();
        // debug dump
//        Log.debug("refreshToken:" + ret);
        // debug dump
//        if (credential != null) {
//            Log.debug("AccessToken:" + credential.getAccessToken());
//        }

        return credential;
    }
    //    Get service
    private static SpreadsheetService getService() throws Exception {
        SpreadsheetService service = new SpreadsheetService(APPLICATION_NAME);
        service.setProtocolVersion(SpreadsheetService.Versions.V3);
        Credential credential = authorize();
        service.setOAuth2Credentials(credential);
        // debug dump
//        Log.debug("Schema: " + service.getSchema().toString());
//        Log.debug("Protocol: " + service.getProtocolVersion().getVersionString());
//        Log.debug("Service Version: " + service.getServiceVersion());

        return service;
    }
   private static File getP12File(){
        ClassLoader classLoader = GoogleSheetsWriter.class.getClassLoader();
        return new File(classLoader.getResource("DriveAPITest-bf290e0ee314.p12").getFile());
    }

相关内容

  • 没有找到相关文章

最新更新