Stored Credential from google api to be reused java

北慕城南 提交于 2019-12-04 00:36:19

问题


I have successfully authorized my desktop application. It stores a file called StoredCredential. In order to not have to keep doing the copy URL into browser, accept, etc., steps every time I run the app, I want to use the credentials that I already have stored.

My code so far:

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 do I read the stored credential and bypass the command line prompt?

I was thinking something like:

if (stored cred exists) { 
    try  {
        // use 
    } catch  {
        // prompt the user
    }
}

回答1:


You can create GoogleCredential object from stored credentials like this:

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");



回答2:


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;
    }


来源:https://stackoverflow.com/questions/19861178/stored-credential-from-google-api-to-be-reused-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!