use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow in project Saber-Bot by notem.
the class GoogleAuth method newAuthorizationUrl.
/**
* @return
* @throws IOException
*/
public static String newAuthorizationUrl() 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();
return flow.newAuthorizationUrl().setScopes(SCOPES).setAccessType("offline").setClientId(clientSecrets.getDetails().getClientId()).setRedirectUri(clientSecrets.getDetails().getRedirectUris().get(0)).toString();
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow 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.GoogleAuthorizationCodeFlow 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.GoogleAuthorizationCodeFlow 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.GoogleAuthorizationCodeFlow 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");
}
Aggregations