Search in sources :

Example 1 with Builder

use of com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.Builder in project OwlPlug by DropSnorz.

the class AuthenticationService method createAccountAndAuth.

/**
 * Creates a new account by starting the Authentication flow.
 *
 * @throws AuthenticationException if an error occurs during Authentication
 *                                   flow.
 */
public void createAccountAndAuth() throws AuthenticationException {
    String clientId = owlPlugCredentials.getGoogleAppId();
    String clientSecret = owlPlugCredentials.getGoogleSecret();
    ArrayList<String> scopes = new ArrayList<>();
    scopes.add("https://www.googleapis.com/auth/drive");
    scopes.add("https://www.googleapis.com/auth/userinfo.profile");
    try {
        NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        DataStoreFactory dataStore = new JPADataStoreFactory(googleCredentialDAO);
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientId, clientSecret, scopes).setDataStoreFactory(dataStore).setAccessType("offline").setApprovalPrompt("force").build();
        UserAccount userAccount = new UserAccount();
        userAccountDAO.save(userAccount);
        receiver = new LocalServerReceiver();
        AuthorizationCodeInstalledApp authCodeAccess = new AuthorizationCodeInstalledApp(flow, receiver);
        Credential credential = authCodeAccess.authorize(userAccount.getKey());
        Oauth2 oauth2 = new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credential).setApplicationName("OwlPlug").build();
        Userinfoplus userinfo = oauth2.userinfo().get().execute();
        userAccount.setName(userinfo.getName());
        userAccount.setIconUrl(userinfo.getPicture());
        userAccount.setAccountProvider(UserAccountProvider.GOOGLE);
        userAccount.setCredential(googleCredentialDAO.findByKey(userAccount.getKey()));
        userAccountDAO.save(userAccount);
        this.getPreferences().putLong(ApplicationDefaults.SELECTED_ACCOUNT_KEY, userAccount.getId());
    } catch (GeneralSecurityException | IOException e) {
        log.error("Error during authentication", e);
        throw new AuthenticationException(e);
    } finally {
        // Delete accounts without complete setup
        userAccountDAO.deleteInvalidAccounts();
    }
}
Also used : Userinfoplus(com.google.api.services.oauth2.model.Userinfoplus) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) Credential(com.google.api.client.auth.oauth2.Credential) AuthenticationException(com.owlplug.auth.utils.AuthenticationException) Oauth2(com.google.api.services.oauth2.Oauth2) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) JPADataStoreFactory(com.owlplug.auth.JPADataStoreFactory) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver) IOException(java.io.IOException) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) UserAccount(com.owlplug.auth.model.UserAccount) DataStoreFactory(com.google.api.client.util.store.DataStoreFactory) JPADataStoreFactory(com.owlplug.auth.JPADataStoreFactory)

Example 2 with Builder

use of com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.Builder 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 3 with Builder

use of com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.Builder in project mucommander by mucommander.

the class GoogleDriveClient method getCredentials.

/**
 * Creates an authorized Credential object.
 */
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT, String host, LocalServerReceiver receiver) throws IOException {
    // Load client secrets.
    Details details = new Details();
    details.setClientId(CLIENT_ID);
    details.setClientSecret(CLIENT_SECRET);
    details.setAuthUri("https://accounts.google.com/o/oauth2/auth");
    details.setTokenUri("https://oauth2.googleapis.com/token");
    details.setRedirectUris(Arrays.asList("urn:ietf:wg:oauth:2.0:oob", "http://localhost"));
    GoogleClientSecrets clientSecrets = new GoogleClientSecrets().setInstalled(details);
    GoogleAuthorizationCodeFlow.Builder builder = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES);
    if (host != null) {
        String tokensDir = getCredentialsFolder().getAbsolutePath();
        builder.setDataStoreFactory(tokensDir != null ? new FileDataStoreFactory(new java.io.File(tokensDir)) : null);
    }
    builder.setAccessType("offline");
    GoogleAuthorizationCodeFlow flow = builder.build();
    if (receiver == null)
        receiver = new LocalServerReceiver();
    return new AuthorizationCodeInstalledApp(flow, receiver, DesktopManager::browse).authorize(host);
}
Also used : FileDataStoreFactory(com.google.api.client.util.store.FileDataStoreFactory) Details(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.Details) 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 Builder

use of com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.Builder in project community by GoogleCloudPlatform.

the class SimpleExample method authorize.

private Credential authorize() throws Exception {
    HttpExecuteInterceptor credentials = new ClientParametersAuthentication(this.clientId, this.clientSecret);
    AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(authorizationHeaderAccessMethod(), httpTransport, jsonFactory, new GenericUrl(TOKEN_SERVER_URL), credentials, this.clientId, AUTHORIZATION_SERVER_URL).setScopes(SCOPES).setDataStoreFactory(dataStoreFactory).build();
    LocalServerReceiver.Builder builder = new LocalServerReceiver.Builder();
    VerificationCodeReceiver receiver = builder.setHost(DOMAIN).setPort(PORT).build();
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
Also used : ClientParametersAuthentication(com.google.api.client.auth.oauth2.ClientParametersAuthentication) VerificationCodeReceiver(com.google.api.client.extensions.java6.auth.oauth2.VerificationCodeReceiver) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver) AuthorizationCodeFlow(com.google.api.client.auth.oauth2.AuthorizationCodeFlow)

Example 5 with Builder

use of com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.Builder in project OsmAnd-tools by osmandapp.

the class UpdateSubscription method getPublisherApi.

public static AndroidPublisher getPublisherApi(String file) throws JSONException, IOException, GeneralSecurityException {
    List<String> scopes = new ArrayList<String>();
    scopes.add("https://www.googleapis.com/auth/androidpublisher");
    File dataStoreDir = new File(new File(file).getParentFile(), ".credentials");
    JacksonFactory jsonFactory = new com.google.api.client.json.jackson2.JacksonFactory();
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(new FileInputStream(file)));
    NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, clientSecrets, scopes).setDataStoreFactory(new FileDataStoreFactory(dataStoreDir)).setAccessType("offline").build();
    Builder bld = new LocalServerReceiver.Builder();
    bld.setPort(5000);
    // it only works with localhost ! with other hosts gives incorrect redirect uri (looks like it's not supported for service accounts)
    // bld.setHost(serverPublicUrl);
    Credential credential = new AuthorizationCodeInstalledApp(flow, bld.build()).authorize("user");
    System.out.println("Credentials saved to " + dataStoreDir.getAbsolutePath());
    AndroidPublisher publisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential).setApplicationName(GOOGLE_PRODUCT_NAME).build();
    return publisher;
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) InputStreamReader(java.io.InputStreamReader) AndroidPublisher(com.google.api.services.androidpublisher.AndroidPublisher) Builder(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.Builder) ArrayList(java.util.ArrayList) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) FileInputStream(java.io.FileInputStream) FileDataStoreFactory(com.google.api.client.util.store.FileDataStoreFactory) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) File(java.io.File)

Aggregations

AuthorizationCodeInstalledApp (com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp)5 LocalServerReceiver (com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)4 GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)4 Credential (com.google.api.client.auth.oauth2.Credential)3 GoogleClientSecrets (com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets)3 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)2 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)2 FileDataStoreFactory (com.google.api.client.util.store.FileDataStoreFactory)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 AuthorizationCodeFlow (com.google.api.client.auth.oauth2.AuthorizationCodeFlow)1 ClientParametersAuthentication (com.google.api.client.auth.oauth2.ClientParametersAuthentication)1 VerificationCodeReceiver (com.google.api.client.extensions.java6.auth.oauth2.VerificationCodeReceiver)1 Builder (com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.Builder)1 Details (com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.Details)1 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)1 DataStoreFactory (com.google.api.client.util.store.DataStoreFactory)1 AndroidPublisher (com.google.api.services.androidpublisher.AndroidPublisher)1 Oauth2 (com.google.api.services.oauth2.Oauth2)1