Example usage for com.google.api.client.googleapis.auth.oauth2 GoogleCredential GoogleCredential

List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleCredential GoogleCredential

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.auth.oauth2 GoogleCredential GoogleCredential.

Prototype

public GoogleCredential() 

Source Link

Document

Constructor with the ability to access protected resources, but not refresh tokens.

Usage

From source file:test.aknahs.com.droiddrivereplace.DriveUtils.java

public static Drive connect() throws IOException {

    Log.v(TAG, "DriveUtils.connect()");
    if (gotService())
        return service;

    Thread thr = new Thread(new Runnable() {

        @Override/*from   w ww . j  a  v a 2 s . c  o m*/
        public void run() {
            try {
                response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();

                credential = new GoogleCredential().setFromTokenResponse(response);

                //Create a new authorized API client
                service = new Drive.Builder(httpTransport, jsonFactory, credential).build();

                Log.v(TAG, "Storing token : " + code);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    thr.start();

    try {
        Log.v(TAG, "thr.join on connect");
        thr.join();

    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    Log.v(TAG, "Captured code : " + code);

    return service;
}

From source file:verifytoken.Verify.java

License:Open Source License

private static VerificationResponse verify(String idToken, String accessToken) {

    TokenStatus idStatus = new TokenStatus();
    if (idToken != null) {
        // Check that the ID Token is valid.

        Checker checker = new Checker(new String[] { CLIENT_ID }, CLIENT_ID);
        GoogleIdToken.Payload jwt = checker.check(idToken);

        if (jwt == null) {
            // This is not a valid token.
            idStatus.setValid(false);//from ww  w. j a v  a2  s  .c om
            idStatus.setId("");
            idStatus.setMessage("Invalid ID Token.");
        } else {
            idStatus.setValid(true);
            idStatus.setId((String) jwt.get("sub"));
            idStatus.setEmail((String) jwt.get("email"));
            idStatus.setMessage("ID Token is valid.");
        }
    } else {
        idStatus.setMessage("ID Token not provided");
    }

    TokenStatus accessStatus = new TokenStatus();
    if (accessToken != null) {
        // Check that the Access Token is valid.
        try {
            GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
            Oauth2 oauth2 = new Oauth2.Builder(TRANSPORT, JSON_FACTORY, credential).build();
            Tokeninfo tokenInfo = oauth2.tokeninfo().setAccessToken(accessToken).execute();

            if (!tokenInfo.getIssuedTo().equals(CLIENT_ID)) {
                // This is not meant for this app. It is VERY important to check
                // the client ID in order to prevent man-in-the-middle attacks.
                accessStatus.setValid(false);
                accessStatus.setId("");
                accessStatus.setMessage("Access Token not meant for this app.");
            } else {
                accessStatus.setValid(true);
                accessStatus.setId(tokenInfo.getUserId());
                accessStatus.setMessage("Access Token is valid.");
            }
        } catch (IOException e) {
            accessStatus.setValid(false);
            accessStatus.setId("");
            accessStatus.setMessage("Invalid Access Token.");
        }
    } else {
        accessStatus.setMessage("Access Token not provided");
    }

    return new VerificationResponse(idStatus, accessStatus);
}