Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package gnomezgrave.gsyncj.auth; import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.drive.Drive; import com.google.api.services.drive.DriveScopes; import java.io.IOException; import java.util.Arrays; /** * * @author praneeth */ public class Authorization { private static final String CLIENT_ID = "819345528050-o48vejgcpjh2kc2vodlasovgk9k4l7kv.apps.googleusercontent.com"; private static final String CLIENT_SECRET = "hJ5RXfb7lmOEnHKgP2n0kj7N"; private static final String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob"; public static synchronized Drive getDrive(String key) throws IOException { HttpTransport httpTransport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE)).setAccessType("online") .setApprovalPrompt("auto").build(); String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build(); System.out.println(url); GoogleTokenResponse response = flow.newTokenRequest(key).setRedirectUri(REDIRECT_URI).execute(); GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response); //Create a new authorized API client return new Drive.Builder(httpTransport, jsonFactory, credential).build(); } public static String getAuthURL() { HttpTransport httpTransport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE)).setAccessType("online") .setApprovalPrompt("auto").build(); return flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build(); } }