use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow 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;
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow 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;
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow in project teammates by TEAMMATES.
the class GmailServiceMaker method authorizeAndCreateCredentials.
/**
* Authorizes the user and creates an authorized Credential.
* @return an authorized Credential
*/
private Credential authorizeAndCreateCredentials() throws IOException {
GoogleClientSecrets clientSecrets = loadClientSecretFromJson();
GoogleAuthorizationCodeFlow flow = buildFlow(clientSecrets);
if (shouldUseFreshCredentials) {
flow.getCredentialDataStore().delete(username);
}
if (flow.getCredentialDataStore().get(username) == null) {
System.out.println("Please login as: " + username);
}
return getCredentialFromFlow(flow);
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow in project teammates by TEAMMATES.
the class GmailServiceMaker method buildFlow.
private GoogleAuthorizationCodeFlow buildFlow(GoogleClientSecrets clientSecrets) throws IOException {
// if the scopes need to change, the user will need to manually delete
// <TestProperties.TEST_GMAIL_API_FOLDER>/StoredCredential
final List<String> scopes = Arrays.asList(GmailScopes.GMAIL_READONLY, GmailScopes.GMAIL_MODIFY);
FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(new File(TestProperties.TEST_GMAIL_API_FOLDER));
return new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setDataStoreFactory(dataStoreFactory).setAccessType("offline").build();
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow in project jbpm-work-items by kiegroup.
the class GoogleCalendarAuth method authorize.
public Credential authorize(String clientSecretJSON) throws Exception {
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new StringReader(clientSecretJSON));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Collections.singleton(CalendarScopes.CALENDAR)).build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
Aggregations