I have registered a web application at cloud.google.com. \"The OAuth 2.0 Client ID\" looks like this:
https://github.com/manishkbharti/grailsOauthPluginDemo already update the way to use Oauth2 for google. If you can't use Oauth2 from this Demo, you can look at grails plugin: Google for Spring Security OAuth plugin (https://github.com/donbeave/grails-spring-security-oauth-google). In their source code, they try to implement Oauth2 from DefaultApi20 class
I don't know why but I can only get the authorization code after the process, so I need to do an extra step to convert this code to an access token, here is the code in details:
def oauth2callback = { def code = params.code
HttpTransport transport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
String CLIENT_ID = "....";
String CLIENT_SECRET = "....";
String REDIRECT_URI = ".....";
GoogleTokenResponse response =
new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, CLIENT_ID, CLIENT_SECRET, code, REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential.Builder().setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.setJsonFactory(jsonFactory).setTransport(transport).build()
.setAccessToken(response.getAccessToken()).setRefreshToken(response.getRefreshToken());
SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1");
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();
spreadsheets.each {
println it.getTitle().getPlainText()
}
}