Search in sources :

Example 1 with GoogleClientSecrets

use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project OsmAnd-tools by osmandapp.

the class ExceptionAnalyzerMain method authorize.

/**
 * Creates an authorized Credential object.
 * @return an authorized Credential object.
 * @throws IOException
 */
public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = ExceptionAnalyzerMain.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;
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)

Example 2 with GoogleClientSecrets

use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project DisCal-Discord-Bot by NovaFox161.

the class CalendarAuth method authorize.

/**
 * Creates an authorized Credential object.
 *
 * @return an authorized Credential object.
 * @throws IOException In the event authorization fails.
 */
private static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in = CalendarAuth.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");
    Logger.getLogger().debug("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}
Also used : GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Credential(com.google.api.client.auth.oauth2.Credential) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)

Example 3 with GoogleClientSecrets

use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project selenium_java by sergueik.

the class Quickstart method authorize.

/**
 * Creates an authorized Credential object.
 * @return an authorized Credential object.
 * @throws IOException
 */
public static Credential authorize() throws IOException {
    // Load client secrets.
    String fileName = "client_secret.json";
    InputStream in = Quickstart.class.getResourceAsStream("/client_secret.json");
    /*
		in = Files.newInputStream(Paths.get(String.format("%s/src/main/resources/%s", System.getProperty("user.dir"), fileName)));
		*/
    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();
    /*
		Exception in thread "main" java.lang.IllegalArgumentException
        at com.google.api.client.repackaged.com.google.common.base.Preconditions
.checkArgument(Preconditions.java:111)
        at com.google.api.client.util.Preconditions.checkArgument(Preconditions.
java:37)
        at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.getD
etails(GoogleClientSecrets.java:82)
        at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeF
low$Builder.<init>(GoogleAuthorizationCodeFlow.java:195)
        at com.github.sergueik.Quickstart.authorize(Quickstart.java:82)
        at com.github.sergueik.Quickstart.getSheetsService(Quickstart.java:99)
        at com.github.sergueik.Quickstart.main(Quickstart.java:106) 
		 */
    Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)

Example 4 with GoogleClientSecrets

use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project tutorials by eugenp.

the class GoogleAuthorizeUtil method authorize.

public static Credential authorize() throws IOException, GeneralSecurityException {
    InputStream in = GoogleAuthorizeUtil.class.getResourceAsStream("/google-sheets-client-secret.json");
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in));
    List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), clientSecrets, scopes).setDataStoreFactory(new MemoryDataStoreFactory()).setAccessType("offline").build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    return credential;
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) MemoryDataStoreFactory(com.google.api.client.util.store.MemoryDataStoreFactory) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)

Example 5 with GoogleClientSecrets

use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project opencast by opencast.

the class OAuth2CredentialFactoryImpl method getGoogleCredential.

@Override
public GoogleCredential getGoogleCredential(final DataStore<StoredCredential> datastore, final ClientCredentials authContext) throws IOException {
    final GoogleCredential gCred;
    final LocalServerReceiver localReceiver = new LocalServerReceiver();
    final String accessToken;
    final String refreshToken;
    try {
        // Reads the client id and client secret from a file name passed in authContext
        final GoogleClientSecrets gClientSecrets = GoogleClientSecrets.load(new JacksonFactory(), new FileReader(authContext.getClientSecrets()));
        // Obtain tokens from credential in data store, or obtains a new one from
        // Google if one doesn't exist
        final StoredCredential sCred = datastore.get(authContext.getClientId());
        if (sCred != null) {
            accessToken = sCred.getAccessToken();
            refreshToken = sCred.getRefreshToken();
            logger.debug(MessageFormat.format("Found credential for client {0} in data store {1}", authContext.getClientId(), datastore.getId()));
        } else {
            // This flow supports installed applications
            final GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(new NetHttpTransport(), new JacksonFactory(), gClientSecrets, authContext.getScopes()).setCredentialDataStore(datastore).setApprovalPrompt("auto").setAccessType("offline").build();
            final Credential cred = new AuthorizationCodeInstalledApp(flow, localReceiver).authorize(authContext.getClientId());
            accessToken = cred.getAccessToken();
            refreshToken = cred.getRefreshToken();
            logger.debug(MessageFormat.format("Created new credential for client {0} in data store {1}", authContext.getClientId(), datastore.getId()));
        }
        gCred = new GoogleCredential.Builder().setClientSecrets(gClientSecrets).setJsonFactory(new JacksonFactory()).setTransport(new NetHttpTransport()).build();
        gCred.setAccessToken(accessToken);
        gCred.setRefreshToken(refreshToken);
        logger.debug(MessageFormat.format("Found credential {0} using {1}", gCred.getRefreshToken(), authContext.toString()));
    } finally {
        localReceiver.stop();
    }
    return gCred;
}
Also used : GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) StoredCredential(com.google.api.client.auth.oauth2.StoredCredential) Credential(com.google.api.client.auth.oauth2.Credential) StoredCredential(com.google.api.client.auth.oauth2.StoredCredential) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) FileReader(java.io.FileReader)

Aggregations

GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)26 GoogleClientSecrets (com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets)26 AuthorizationCodeInstalledApp (com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp)20 LocalServerReceiver (com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)20 Credential (com.google.api.client.auth.oauth2.Credential)10 InputStreamReader (java.io.InputStreamReader)9 FileDataStoreFactory (com.google.api.client.util.store.FileDataStoreFactory)6 StringReader (java.io.StringReader)6 InputStream (java.io.InputStream)5 File (java.io.File)4 StoredCredential (com.google.api.client.auth.oauth2.StoredCredential)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)3 Details (com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.Details)2 FileInputStream (java.io.FileInputStream)2 FileReader (java.io.FileReader)2 Reader (java.io.Reader)2 GoogleTokenResponse (com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 GsonFactory (com.google.api.client.json.gson.GsonFactory)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1