使用OAuth 2.0进行Java和Google Spreadsheets API授权

我想使用Java阅读Google Spreadsheets,推荐的方法是使用Google Spreadsheets API 。

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

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

请相信任何建议。

Google Data Java Client Library现在支持OAuth 2.0:

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

不幸的是,库中没有完整的样本显示如何使用它。 我建议检查这两个链接,将信息放在一起使其工作:

我还发现开发人员文档为除OAuth2之外的所有内容提供了Java示例,这非常愚蠢。 这是我用来使其工作的一些示例代码。 为了完整起见,它包括后面部分中的检索电子表格示例 。 另请注意,您必须将所需的范围添加到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 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()); } } } 

[编辑]

Java OAuth2代码

博客post在[google-spreadsheet-api]和OAuth2上,带有代码
http://soatutorials.blogspot.co.at/2013/08/google-spreadsheet-api-connecting-with.html

相关问题: 使用google gdata客户端API从Java / Scala获得OAuth2授权

[结束编辑]

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

额外注意:更好的文档API是Google-Apps-Script。