use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project Saber-Bot by notem.
the class GoogleAuth method unauthorize.
/**
* removes stored authorization token for a user
* @param userID discord ID of user
* @throws IOException
*/
public static void unauthorize(String userID) throws IOException {
// Load client secrets.
InputStream in = new FileInputStream(Main.getBotSettingsManager().getGoogleOAuthSecret());
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();
flow.getCredentialDataStore().delete(userID);
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project Saber-Bot by notem.
the class GoogleAuth method authorize.
/**
* Creates a new authorized Credential object from a response token
* @param token (String)
* @param userId (String)
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize(String token, String userId) throws IOException {
// Load client secrets.
InputStream in = new FileInputStream(Main.getBotSettingsManager().getGoogleOAuthSecret());
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();
// remove any account previously associated with the token
flow.getCredentialDataStore().delete(userId);
// create the new credential
GoogleTokenResponse response = flow.newTokenRequest(token).setRedirectUri(clientSecrets.getDetails().getRedirectUris().get(0)).execute();
return flow.createAndStoreCredential(response, userId);
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project api-samples by youtube.
the class Quickstart method authorize.
/**
* Create an authorized Credential object.
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in = Quickstart.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");
return credential;
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project textdb by TextDB.
the class GoogleResource method createCredential.
/**
* Creates an authorized Credential object for the service account
* Copied from google sheet API (https://developers.google.com/sheets/api/quickstart/java)
* rewrite this part when migrate to user's account
*/
private static Credential createCredential() throws IOException, GeneralSecurityException {
// Load client secrets
File initialFile = new File(CREDENTIALS_FILE_PATH);
InputStream targetStream = new FileInputStream(initialFile);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(targetStream));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(getHttpTransport(), JSON_FACTORY, clientSecrets, SCOPES).setDataStoreFactory(new FileDataStoreFactory(new File(TOKENS_DIRECTORY_PATH))).setAccessType("offline").build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project Ardent by adamint.
the class Ardent method authorize.
public static Credential authorize() throws IOException {
// Load client secrets.
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, new StringReader(clientSecret));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, clientSecrets, scopes).setDataStoreFactory(dataStoreFactory).build();
Credential credential;
if (!testingBot)
credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver.Builder().setHost("ardentbot.tk").setPort(1337).build()).authorize("703818195441-no5nh31a0rfcogq8k9ggsvsvkq5ai0ih.apps.googleusercontent.com");
else {
credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver.Builder().setHost("localhost").setPort(1337).build()).authorize("703818195441-no5nh31a0rfcogq8k9ggsvsvkq5ai0ih.apps.googleusercontent.com");
}
// System.out.println(credential.refreshToken());
return credential;
}
Aggregations