Example usage for com.google.api.client.googleapis.auth.oauth2 GoogleClientSecrets.Details set

List of usage examples for com.google.api.client.googleapis.auth.oauth2 GoogleClientSecrets.Details set

Introduction

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

Prototype

@Override
    public GoogleClientSecrets set(String fieldName, Object value) 

Source Link

Usage

From source file:net.starschema.clouddb.cmdlineverification.Oauth2Bigquery.java

License:Open Source License

/**
 * Authorizes the installed application to access user's protected data. if
 * possible, gets the credential from xml file at PathForXmlStore
 *
 * @param transport   HTTP transport//from ww w . j  a v  a2s. co  m
 * @param jsonFactory JSON factory
 * @param receiver    verification code receiver
 * @param scopes      OAuth 2.0 scopes
 */
public static Credential authorize(HttpTransport transport, JsonFactory jsonFactory,
        VerificationCodeReceiver receiver, List<String> scopes, String clientid, String clientsecret)
        throws Exception {

    BQXMLCredentialStore Store = new BQXMLCredentialStore(Oauth2Bigquery.PathForXmlStore);

    GoogleClientSecrets.Details details = new Details();
    details.setClientId(clientid);
    details.setClientSecret(clientsecret);
    details.set("Factory", CmdlineUtils.getJsonFactory());
    details.setAuthUri("https://accounts.google.com/o/oauth2/auth");
    details.setTokenUri("https://accounts.google.com/o/oauth2/token");
    GoogleClientSecrets secr = new GoogleClientSecrets().setInstalled(details);
    GoogleCredential CredentialForReturn = new GoogleCredential.Builder()
            .setJsonFactory(CmdlineUtils.getJsonFactory()).setTransport(CmdlineUtils.getHttpTransport())
            .setClientSecrets(secr).build();

    if (Store.load(clientid + ":" + clientsecret, CredentialForReturn) == true) {
        return CredentialForReturn;
    }
    try {
        String redirectUri = receiver.getRedirectUri();
        GoogleClientSecrets clientSecrets = Oauth2Bigquery.loadClientSecrets(jsonFactory, clientid,
                clientsecret);
        Oauth2Bigquery.codeflow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, clientSecrets,
                scopes).setAccessType("offline").setApprovalPrompt("auto").setCredentialStore(Store).build();
        Oauth2Bigquery
                .browse(Oauth2Bigquery.codeflow.newAuthorizationUrl().setRedirectUri(redirectUri).build());
        // receive authorization code and exchange it for an access token
        String code = receiver.waitForCode();
        GoogleTokenResponse response = Oauth2Bigquery.codeflow.newTokenRequest(code).setRedirectUri(redirectUri)
                .execute();
        // store credential and return it

        // Also ads a RefreshListener, so the token will be always
        // automatically refreshed.
        return Oauth2Bigquery.codeflow.createAndStoreCredential(response, clientid + ":" + clientsecret);
    } finally {
        receiver.stop();
    }
}