Saved credentials from google api to reuse java

I successfully resolved my desktop application. It stores a file with a name StoredCredential. So that there is no need to make the copy URL in the browser, accept, etc., Each time I launch the application, I want to use the credentials that I already saved.

My code is:

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
    httpTransport,
    JSON_FACTORY,
    CLIENT_ID,
    CLIENT_SECRET,
    SCOPE)
    .setDataStoreFactory(dataStoreFactory)
    .setApprovalPrompt("auto").setAccessType("offline").build();
//
System.out.println("flow success");     

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 tokenResponse
    = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();

GoogleCredential credential = new GoogleCredential
    .Builder()
    .setTransport(new NetHttpTransport())
    .setJsonFactory(new JacksonFactory())
    .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
    .addRefreshListener(new CredentialRefreshListener() {
            @Override
            public void onTokenResponse(
                Credential credential,
                TokenResponse tokenResponse) throws IOException {

                System.out.println("Token Refreshed Succesfully");
            }

            @Override
            public void onTokenErrorResponse(
                Credential credential,
                TokenErrorResponse tokenErrorResponse) throws IOException {

                System.out.println("ERROR WITH TOKEN WTF?");
                }})
     .build();

How to view saved credentials and bypass the command line prompt?

I thought something like:

if (stored cred exists) { 
    try  {
        // use 
    } catch  {
        // prompt the user
    }
}
+10
source share
2 answers

You can create a GoogleCredential object from stored credentials, for example:

HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();
        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setClientSecrets("client_id", "client_secret").build();
        credential.setAccessToken("access_token");
+14
source
buildEmptyCredential()
                .setRefreshToken(storedCredential.getRefreshToken())
                .setAccessToken(storedCredential.getAccessToken())
                .setExpirationTimeMilliseconds(storedCredential.getExpirationTimeMilliseconds());

public Credential buildEmptyCredential() {
        try {
            return new GoogleCredential.Builder()
                    .setClientSecrets(Utils.getClientCredential())
                    .setTransport(new NetHttpTransport())
                    .setJsonFactory(Constant.JSON_FACTORY).build();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
+2
source

Source: https://habr.com/ru/post/1543912/


All Articles