use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project HearthStats.net-Uploader by HearthStats.
the class Auth method authorize.
/**
* Authorizes the installed application to access user's protected data.
*
* @param scopes list of scopes needed to run youtube upload.
* @param credentialDatastore name of the credential datastore to cache OAuth tokens
*/
public static Credential authorize(List<String> scopes, String credentialDatastore) throws IOException {
// Load client secrets.
Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/client_secrets.json"));
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader);
// Checks that the defaults have been replaced (Default = "Enter X here").
if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/?api=youtube" + "into src/main/resources/client_secrets.json");
System.exit(1);
}
// This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY));
DataStore<StoredCredential> datastore = fileDataStoreFactory.getDataStore(credentialDatastore);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialDataStore(datastore).build();
// Build the local server and bind it to port 8080
LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();
// Authorize.
return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project drbookings by DrBookings.
the class RunnableImportGoogleCalendar method authorize.
/**
* Authorizes the installed application to access user's protected data.
*/
private static Credential authorize() throws Exception {
// load client secrets
final GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(RunnableImportGoogleCalendar.class.getResourceAsStream("/client_secrets.json")));
if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/?api=calendar " + "into calendar-cmdline-sample/src/main/resources/client_secrets.json");
System.exit(1);
}
// set up authorization code flow
final GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(dataStoreFactory).build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project google-api-java-client by google.
the class GoogleClientSecretsTest method testLoad.
public void testLoad() throws Exception {
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(new GsonFactory(), new StringReader(CLIENT_SECRETS));
Details installed = clientSecrets.getInstalled();
assertNotNull(installed);
assertEquals(CLIENT_ID, installed.getClientId());
assertEquals(CLIENT_SECRET, installed.getClientSecret());
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project repairnator by Spirals-Team.
the class ManageGoogleAccessToken method initializeCredentialFromGoogleSecret.
public void initializeCredentialFromGoogleSecret(String googleSecretPath) throws IOException {
this.dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
File secretFile = new File(googleSecretPath);
if (!secretFile.exists()) {
throw new IOException("File containing the token information to access Google API does not exist.");
}
// Load client secrets.
InputStream in = new FileInputStream(secretFile);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(this.jsonFactory, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(this.httpTransport, this.jsonFactory, clientSecrets, this.scopes).setDataStoreFactory(this.dataStoreFactory).setAccessType("offline").build();
this.credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
this.logger.info("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
}
use of com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets in project gradle-play-publisher by ZeroBrain.
the class AndroidPublisherHelper method authorizeWithInstalledApplication.
/**
* Authorizes the installed application to access user's protected data.
*
* @throws java.io.IOException
* @throws java.security.GeneralSecurityException
*/
private static Credential authorizeWithInstalledApplication(File secretFile, File authStore) throws IOException {
log.info("Authorizing using installed application");
// load client secrets
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new FileReader(secretFile));
// Ensure file has been filled out.
checkClientSecretsFile(clientSecrets);
dataStoreFactory = new FileDataStoreFactory(authStore);
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)).setDataStoreFactory(dataStoreFactory).build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize(INST_APP_USER_ID);
}
Aggregations