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

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

Introduction

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

Prototype

public static GoogleClientSecrets load(JsonFactory jsonFactory, Reader reader) throws IOException 

Source Link

Document

Loads the client_secrets.json file from the given reader.

Usage

From source file:ch.chnoch.appengine.bunddownloader.CredentialMediator.java

License:Apache License

public CredentialMediator(InputStream clientSecretsStream, Collection<String> scopes)
        throws InvalidClientSecretsException {
    this.scopes = scopes;
    this.credentialStore = new AppEngineCredentialStore();
    try {//from w  ww.  ja va  2s .c  om
        secrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretsStream);
    } catch (IOException e) {
        throw new InvalidClientSecretsException("client_secrets.json is missing or invalid.");
    }
}

From source file:clientevideo.ClienteVideo.java

private static Credential authorize() throws Exception {
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(ClienteVideo.class.getResourceAsStream("client_secret.json")));

    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, Collections.singleton(DriveScopes.DRIVE_FILE))
                    .setDataStoreFactory(DATA_STORE_FACTORY).build();

    // authorize//from ww  w.  j a v a2  s.  c o m
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("domain");
}

From source file:cmg.org.monitor.services.GooglePlusService.java

License:Open Source License

/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            GooglePlusService.class.getResourceAsStream("/cmg/org/monitor/client/secrets/client_secrets.json"));

    // set up file credential store
    FileCredentialStore credentialStore = new FileCredentialStore(new File("D:/plus.json"), JSON_FACTORY);
    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, Collections.singleton(PlusScopes.PLUS_ME)).setCredentialStore(credentialStore)
                    .build();/*from ww w .ja  va  2s .c  o m*/
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

From source file:com.aalmeida.lightning.external.auth.GoogleAuthorizationImpl.java

License:Apache License

/**
 * Authorize.//  w  ww.j  a va  2  s  .c  om
 *
 * @param pUrl
 *            the url
 * @return the credential
 * @throws Exception
 *             the exception
 */
private static Credential authorize(String pUrl) throws Exception {
    URL url = new URL(pUrl);
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, in);
    String clientId = clientSecrets.getDetails().getClientId();
    String clientSecret = clientSecrets.getDetails().getClientSecret();
    if ((clientId == null) || (clientId.isEmpty()) || (clientSecret == null) || (clientSecret.isEmpty())) {
        throw new Exception("Empty Google credencials");
    }
    GoogleClientSecrets.Details details = new GoogleClientSecrets.Details();
    details.setClientId(clientId);
    details.setClientSecret(clientSecret);
    clientSecrets.setInstalled(details);

    List<String> permissions = new ArrayList<String>();
    permissions.add("https://www.googleapis.com/auth/calendar");
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory,
            clientSecrets, permissions).setAccessType("offline").setApprovalPrompt("force")
                    .setDataStoreFactory(dataStoreFactory).build();

    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

From source file:com.abubusoft.liferay.google.GoogleOAuth.java

License:Open Source License

protected GoogleAuthorizationCodeFlow getFlow(long companyId) throws Exception {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    InputStream is = GoogleOAuth.class.getResourceAsStream(_CLIENT_SECRETS_LOCATION);

    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(is));

    //clientSecrets.getDetails().      
    String apiKey = PrefsPropsUtil.getString(companyId, "google.api.key");
    String apiSecret = PrefsPropsUtil.getString(companyId, "google.api.secret");
    String apiCallbackURL = PrefsPropsUtil.getString(companyId, "google.api.callback.url");

    if (apiKey != null && apiKey.trim().length() > 0) {
        clientSecrets.getDetails().setClientId(apiKey);
    }/*from ww w . j  a  v  a  2  s .co  m*/
    if (apiSecret != null && apiSecret.trim().length() > 0) {
        clientSecrets.getDetails().setClientSecret(apiSecret);
    }
    if (apiCallbackURL != null && apiCallbackURL.trim().length() > 0) {
        clientSecrets.getDetails().getRedirectUris().add(apiCallbackURL);
    }

    GoogleAuthorizationCodeFlow.Builder builder = new GoogleAuthorizationCodeFlow.Builder(httpTransport,
            jsonFactory, clientSecrets, _SCOPES);

    builder.setAccessType("online");
    builder.setApprovalPrompt("force");

    return builder.build();
}

From source file:com.appspot.direct_viewer.AbstractServlet.java

License:Apache License

/**
 * Reads client_secrets.json and creates a GoogleClientSecrets object.
 * //w  ww.j  av a2  s  . c om
 * @return A GoogleClientsSecrets object.
 */
private GoogleClientSecrets getClientSecrets() {
    // TODO: do not read on each request
    InputStream stream = getServletContext().getResourceAsStream(CLIENT_SECRETS_FILE_PATH);
    try {
        return GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(stream));
    } catch (IOException e) {
        throw new RuntimeException("No client_secrets.json found");
    }
}

From source file:com.blitz.picasaclient.CredentialsProvider.java

License:Apache License

/**
 * Authorizes the installed application to access user's protected data.
 * <p>/*from   w w w.j a  v  a2 s . c o  m*/
 * <p>If you plan to run on AppEngine or Compute Engine, consider instead
 * {@link GoogleCredential#getApplicationDefault()}, which will use the ambient credentials
 * for the project's service-account.
 */
public static Credential authorize(HttpTransport httpTransport, JsonFactory jsonFactory, Stage stage)
        throws IOException, GeneralSecurityException {
    dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory,
            new InputStreamReader(CredentialsProvider.class.getResourceAsStream("/client_secrets.json")));
    if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
        System.out.println(
                "Enter Client ID and Secret from https://console.developers.google.com/project/_/apiui/"
                        + "credential into storage-cmdline-sample/src/main/resources/client_secrets.json");
        System.exit(1);
    }

    // set up authorization code flow
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory,
            clientSecrets,
            Arrays.asList("https://picasaweb.google.com/data/", PlusScopes.PLUS_ME, PlusScopes.USERINFO_EMAIL))
                    .setDataStoreFactory(dataStoreFactory).build();
    // authorize
    return new AuthorizationHelper(flow).authorize(clientSecrets.getDetails().getClientId(), stage);
}

From source file:com.cimat.meetme.validation.implementation.CalendarImportValid.java

License:Apache License

/**
 * Creates an authorized Credential object.
 *
 * @return an authorized Credential object.
 * @throws IOException/*from w ww .  jav a2 s  .c  o m*/
 */
public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = CalendarImportValid.class.getResourceAsStream("/client_secret.json");
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
            .authorize("user");
    System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}

From source file:com.cloudglomerate.connection.OAuth2Native.java

License:Apache License

/**
 * Loads the Google client secrets (if not already loaded).
 *
 * @param jsonFactory JSON factory//w  w  w.  j  av  a2 s.co m
 */
private static GoogleClientSecrets loadClientSecrets(JsonFactory jsonFactory) throws IOException {
    if (clientSecrets == null) {
        InputStream inputStream = OAuth2Native.class.getResourceAsStream(RESOURCE_LOCATION);
        Preconditions.checkNotNull(inputStream, "missing resource %s", RESOURCE_LOCATION);
        clientSecrets = GoogleClientSecrets.load(jsonFactory, inputStream);
        Preconditions.checkArgument(
                !clientSecrets.getDetails().getClientId().startsWith("[[")
                        && !clientSecrets.getDetails().getClientSecret().startsWith("[["),
                "Please enter your client ID and secret from the Google APIs Console in %s from the "
                        + "root samples directory",
                RESOURCE_PATH);
    }
    return clientSecrets;
}

From source file:com.cloudhub.main.Main02.java

License:Apache License

/**
 * Creates an authorized Credential object.
 * @return an authorized Credential object.
 * @throws IOException//from w  w  w. j ava  2s.  co m
 */
public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = Main02.class.getResourceAsStream("/client_secret.json");
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY,
            clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
            .authorize("user");
    System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}