I have writeen some code that is uploading images succesfully in Google drive but I need to copy the authorization code in browser that we getting after run this project and getting the code in browser. I need to copy this code in console then its working fine.
But i am trying to make batch file for uploading images so dont want these manual work. Please help me. Thanks in advance.
Here is my code :
/**
* Client id from GD.
*/
private static String CLIENT_ID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
/**
* Client secret key from GD.
*/
private static String CLIENT_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
/**
* Redirect url from GD.
*/
private static String REDIRECT_URI = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
public static void main(String[] args) 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("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 a new authorized API client
Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();
//Insert a file
File body = new File();
body.setTitle("imagename");
body.setDescription("A test document");
body.setMimeType("image/jpeg");
java.io.File fileContent = new java.io.File("icon.jpg");
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
File file = service.files().insert(body, mediaContent).execute();
System.out.println("File ID: " + file.getId());
You only need to go through the authorization flow the first time you run the app. Then, you can store the refresh token and use it to build the credential object required to authorize your requests. Refresh tokens last until manually revoked by the user.