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"); }
source share