I am developing a Java based desktop application which needs to download some files from the user's Google Drive account. I have studied the Google Drive SDK documentation and so far I have come up with the following code:
public class Main
{
public static void main(String[] args)
{
String clientId = "...";
String clientSecret = "...";
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport,
jsonFactory,
clientId,
clientSecret,
Arrays.asList(DriveScopes.DRIVE)
)
.setAccessType("online")
.setApprovalPrompt("auto").build();
String redirectUri = "urn:ietf:wg:oauth:2.0:oob";
String url =
flow
.newAuthorizationUrl()
.setRedirectUri(redirectUri)
.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(redirectUri)
.execute();
GoogleCredential credential =
new GoogleCredential()
.setFromTokenResponse(response);
Drive service =
new Drive.Builder(httpTransport, jsonFactory, credential)
.build();
...
}
}
This works but it requires the user to authorize the application (i.e. open the given URL in a browser and copy the authorization token) each time. I need to implement the application in a way that would require the user to authorize it when he runs it for the first time only. The application would then store some kind of a secret token locally to be used the next time.
I have studied the documentation thoroughly but I haven't found any sufficient explanation about how to achieve this goal (in a desktop application particularly).
How do I do that?