【问题标题】:Java and Google Spreadsheets API Authorization with OAuth 2.0使用 OAuth 2.0 进行 Java 和 Google 电子表格 API 授权
【发布时间】:2015-05-22 14:54:55
【问题描述】:

我想使用 Java 阅读 Google 电子表格,推荐的方法是使用 Google Spreadsheets API

当您想要使程序安全时,问题就开始了,因此他们鼓励您使用 OAuth 2.0。在 official page 中,他们展示了如何仅使用 .NET 来执行此操作,并说“Java 客户端库当前不支持 OAuth 2.0”,他们提供了使用 OAuth 1.0 或 @ 等替代方法987654325@ 直接使用电子邮件密码

这是肯定的吗?难道没有办法通过Java进行OAuth 2.0身份验证,可能不直接使用Java客户端库,而是通过带有特定参数的请求。

如果有任何建议,我将不胜感激。

【问题讨论】:

标签: java oauth-2.0 google-drive-api google-spreadsheet-api


【解决方案1】:

我还发现 developer docs 为除 OAuth2 之外的所有内容提供 Java 示例非常愚蠢。这是我用来让它工作的一些示例代码。为了完整起见,它在后面的部分中包含了retrieving spreadsheets example。另请注意,您必须将所需的范围添加到 Java DrEdit 示例中,如下所示。

public class GSpreadsheets {

    private static final String CLIENT_ID = "YOUR_CLIENT_ID";
    private static final String CLIENT_SECRET = "YOUR_SECRET_ID";
    private static final String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

    public static void main(String[] args) throws Exception {

        if (CLIENT_ID.equals("YOUR_CLIENT_ID") || CLIENT_SECRET.equals("YOUR_SECRET_ID")) {
            throw new RuntimeException(
                    "TODO: Get client ID and SECRET from https://cloud.google.com/console");
        }

            // get credentials similar to Java DrEdit example
            // https://developers.google.com/drive/examples/java
        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
                Arrays.asList(DriveScopes.DRIVE, 
                              "https://spreadsheets.google.com/feeds", 
                              "https://docs.google.com/feeds"))
                .setAccessType("online")
                .setApprovalPrompt("auto").build();

        String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
        System.out.println("Please open the following URL in your "
                + "browser then type the authorization code:");
        System.out.println("  " + url);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String code = br.readLine();

        GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
        GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);

            // create the service and pass it the credentials you created earlier
        SpreadsheetService service = new SpreadsheetService("MyAppNameHere");
        service.setOAuth2Credentials(credential);

        // Define the URL to request.  This should never change.
        URL SPREADSHEET_FEED_URL = new URL(
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

        // Make a request to the API and get all spreadsheets.
        SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
        List<SpreadsheetEntry> spreadsheets = feed.getEntries();

        // Iterate through all of the spreadsheets returned
        for (SpreadsheetEntry spreadsheet : spreadsheets) {
          // Print the title of this spreadsheet to the screen
          System.out.println(spreadsheet.getTitle().getPlainText());
        }
    }
}

【讨论】:

    【解决方案2】:

    Google Data Java 客户端库现在支持 OAuth 2.0:

    https://code.google.com/p/gdata-java-client/source/detail?r=505

    很遗憾,库中没有完整的示例来展示如何使用它。我建议检查这两个链接以将信息放在一起以使其正常工作:

    【讨论】:

      【解决方案3】:

      [编辑]

      Java OAuth2 代码

      关于 [google-spreadsheet-api] 和 OAuth2 的博文,带有代码
      http://soatutorials.blogspot.co.at/2013/08/google-spreadsheet-api-connecting-with.html

      相关问题: OAuth2 authorization from Java/Scala using google gdata client API

      [结束编辑]

      我使用过:Google drive DrEdit 教程,完整示例展示了如何将 OAuth 2.0 与 Drive 一起使用。该代码适用于谷歌电子表格 GData 样式 API。 (注意:不包括刷新令牌,但刷新令牌按您的预期工作,所以不要太难添加。) -

      额外说明:Google-Apps-Script 是文档化程度更高的 API。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-09
        • 1970-01-01
        • 1970-01-01
        • 2014-10-28
        • 2012-11-12
        • 2014-02-21
        • 1970-01-01
        相关资源
        最近更新 更多