Search in sources :

Example 11 with GoogleAuthorizationCodeFlow

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();
}
Also used : GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets)

Example 12 with GoogleAuthorizationCodeFlow

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);
}
Also used : GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets)

Example 13 with GoogleAuthorizationCodeFlow

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);
}
Also used : GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) GoogleTokenResponse(com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets)

Example 14 with GoogleAuthorizationCodeFlow

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;
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)

Example 15 with GoogleAuthorizationCodeFlow

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");
}
Also used : FileDataStoreFactory(com.google.api.client.util.store.FileDataStoreFactory) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) AuthorizationCodeInstalledApp(com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp) GoogleClientSecrets(com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets) LocalServerReceiver(com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)

Aggregations

GoogleAuthorizationCodeFlow (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow)28 GoogleClientSecrets (com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets)25 AuthorizationCodeInstalledApp (com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp)22 LocalServerReceiver (com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver)22 Credential (com.google.api.client.auth.oauth2.Credential)10 InputStreamReader (java.io.InputStreamReader)9 FileDataStoreFactory (com.google.api.client.util.store.FileDataStoreFactory)7 InputStream (java.io.InputStream)5 StringReader (java.io.StringReader)5 File (java.io.File)4 StoredCredential (com.google.api.client.auth.oauth2.StoredCredential)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)2 FileInputStream (java.io.FileInputStream)2 FileReader (java.io.FileReader)2 Reader (java.io.Reader)2 GoogleTokenResponse (com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 MemoryDataStoreFactory (com.google.api.client.util.store.MemoryDataStoreFactory)1 IOException (java.io.IOException)1