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();
}
}
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;
}
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);
}
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");
}
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;
}
Aggregations