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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations