Java and Google Spreadsheets authorization using OAuth 2.0

I want to read Google spreadsheets using Java, and the recommended way to do this is to use the Google Spreadsheets API .

The problem starts when you want to make procedures safe, so they recommend using OAuth 2.0. On the official page, they show how to do this using only .NET and say that "the Java client library does not currently support OAuth 2.0," and they give alternatives, for example, using OAuth 1.0 or Client Login , using direct email and password.

Is this for sure ?, there is no way to do OAuth 2.0 authentication through Java, perhaps not using the Java client library directly, but through requests with specific parameters.

Please, I would appreciate any suggestions.

+4
source share
3 answers

The Google Data Client Library now supports OAuth 2.0:

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

Unfortunately, the library does not have complete samples showing how to use them. I would recommend checking out these two links to gather information to make it work:

+3
source

I also found it pretty silly that docs docs provided Java examples for everything except OAuth2. Here is an example of the code that I used to make it work. For completeness, it includes extracting spreadsheet examples in the next section. Please also note that you must add the necessary applications to the Java DrEdit example, as shown below.

 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()); } } } 
+8
source

[change]

Java OAuth2 Code

Blog post on [google-spreadsheet-api] and OAuth2 with code
http://soatutorials.blogspot.co.at/2013/08/google-spreadsheet-api-connecting-with.html

Related question: OAuth2 authorization from Java / Scala using the Google Google gdata API

[end edit]

I used: Google DrEdit tutorial , a complete example shows how to use OAuth 2.0 using Drive. The code works with the GData google spreadsheet APIs. (note: does not contain an update token, but the update token works as you expected, so it’s not too difficult to add.) -

Additional note. The best documented API is Google-Apps-Script.

+1
source

All Articles