com.google.api.services.calendar.CalendarScopes Java Examples

The following examples show how to use com.google.api.services.calendar.CalendarScopes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleCalendarAuth.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
public Credential authorize(String clientSecretJSON) throws Exception {
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
                                                                 new StringReader(clientSecretJSON));

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            HTTP_TRANSPORT,
            JSON_FACTORY,
            clientSecrets,
            Collections.singleton(CalendarScopes.CALENDAR))
            .build();

    return new AuthorizationCodeInstalledApp(flow,
                                             new LocalServerReceiver()).authorize("user");
}
 
Example #2
Source File: SyncTokenSample.java    From java-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
  try {
    List<String> scopes = Lists.newArrayList(CalendarScopes.CALENDAR_READONLY);
    client = Utils.createCalendarClient(scopes);
    eventDataStore = Utils.getDataStoreFactory().getDataStore("EventStore");
    syncSettingsDataStore = Utils.getDataStoreFactory().getDataStore("SyncSettings");
    run();
  } catch (Throwable t) {
    t.printStackTrace();
    System.exit(1);
  }
}
 
Example #3
Source File: ConditionalModificationSample.java    From java-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
  try {
    List<String> scopes = Lists.newArrayList(CalendarScopes.CALENDAR);
    client = Utils.createCalendarClient(scopes);
    run();
  } catch (Throwable t) {
    t.printStackTrace();
    System.exit(1);
  }
}
 
Example #4
Source File: ConditionalRetrievalSample.java    From java-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
  try {
    List<String> scopes = Lists.newArrayList(CalendarScopes.CALENDAR);
    client = Utils.createCalendarClient(scopes);
    run();
  } catch (Throwable t) {
    t.printStackTrace();
    System.exit(1);
  }
}
 
Example #5
Source File: GoogleExternalAuth.java    From DisCal-Discord-Bot with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void requestCode(MessageCreateEvent event, GuildSettings settings) {
	try {
		RequestBody body = new FormBody.Builder()
				.addEncoded("client_id", Authorization.getAuth().getClientData().getClientId())
				.addEncoded("scope", CalendarScopes.CALENDAR)
				.build();

		Request httpRequest = new okhttp3.Request.Builder()
				.url("https://accounts.google.com/o/oauth2/device/code")
				.post(body)
				.header("Content-Type", "application/x-www-form-urlencoded")
				.build();

		Response response = Authorization.getAuth().getClient().newCall(httpRequest).execute();

		if (response.code() == HttpStatusCodes.STATUS_CODE_OK) {
			JSONObject codeResponse = new JSONObject(response.body().string());

			//Send DM to user with code.
			Consumer<EmbedCreateSpec> embed = spec -> {
				spec.setAuthor("DisCal", GlobalConst.discalSite, GlobalConst.iconUrl);
				spec.setTitle(MessageManager.getMessage("Embed.AddCalendar.Code.Title", settings));
				spec.addField(MessageManager.getMessage("Embed.AddCalendar.Code.Code", settings), codeResponse.getString("user_code"), true);
				spec.setFooter(MessageManager.getMessage("Embed.AddCalendar.Code.Footer", settings), null);

				spec.setUrl(codeResponse.getString("verification_url"));
				spec.setColor(GlobalConst.discalColor);
			};



			User user = event.getMember().get();
			MessageManager.sendDirectMessageAsync(MessageManager.getMessage("AddCalendar.Auth.Code.Request.Success", settings), embed, user);

			//Start timer to poll Google Cal for auth
			Poll poll = new Poll(user, event.getGuild().block());

			poll.setDevice_code(codeResponse.getString("device_code"));
			poll.setRemainingSeconds(codeResponse.getInt("expires_in"));
			poll.setExpires_in(codeResponse.getInt("expires_in"));
			poll.setInterval(codeResponse.getInt("interval"));
			pollForAuth(poll);
		} else {
			MessageManager.sendDirectMessageAsync(MessageManager.getMessage("AddCalendar.Auth.Code.Request.Failure.NotOkay", settings), event.getMember().get());

			Logger.getLogger().debug(event.getMember().get(), "Error requesting access token.", "Status code: " + response.code() + " | " + response.message() + " | " + response.body().string(), true, this.getClass());
		}
	} catch (Exception e) {
		//Failed, report issue to dev.
		Logger.getLogger().exception(event.getMember().get(), "Failed to request Google Access Code", e, true, this.getClass());
		Member u = event.getMember().get();
		MessageManager.sendDirectMessageAsync(MessageManager.getMessage("AddCalendar.Auth.Code.Request.Failure.Unknown", settings), u);
	}
}
 
Example #6
Source File: GoogleExternalAuth.java    From DisCal-Discord-Bot with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void requestCode(MessageCreateEvent event, GuildSettings settings) {
	try {
		RequestBody body = new FormBody.Builder()
				.addEncoded("client_id", Authorization.getAuth().getClientData().getClientId())
				.addEncoded("scope", CalendarScopes.CALENDAR)
				.build();

		Request httpRequest = new okhttp3.Request.Builder()
				.url("https://accounts.google.com/o/oauth2/device/code")
				.post(body)
				.header("Content-Type", "application/x-www-form-urlencoded")
				.build();

		Response response = Authorization.getAuth().getClient().newCall(httpRequest).execute();

		if (response.code() == HttpStatusCodes.STATUS_CODE_OK) {
			JSONObject codeResponse = new JSONObject(response.body().string());

			//Send DM to user with code.
			Consumer<EmbedCreateSpec> embed = spec -> {
				spec.setAuthor("DisCal", GlobalConst.discalSite, GlobalConst.iconUrl);
				spec.setTitle(MessageManager.getMessage("Embed.AddCalendar.Code.Title", settings));
				spec.addField(MessageManager.getMessage("Embed.AddCalendar.Code.Code", settings), codeResponse.getString("user_code"), true);
				spec.setFooter(MessageManager.getMessage("Embed.AddCalendar.Code.Footer", settings), null);

				spec.setUrl(codeResponse.getString("verification_url"));
				spec.setColor(GlobalConst.discalColor);
			};



			User user = event.getMember().get();
			MessageManager.sendDirectMessageAsync(MessageManager.getMessage("AddCalendar.Auth.Code.Request.Success", settings), embed, user);

			//Start timer to poll Google Cal for auth
			Poll poll = new Poll(user, event.getGuild().block());

			poll.setDevice_code(codeResponse.getString("device_code"));
			poll.setRemainingSeconds(codeResponse.getInt("expires_in"));
			poll.setExpires_in(codeResponse.getInt("expires_in"));
			poll.setInterval(codeResponse.getInt("interval"));
			pollForAuth(poll);
		} else {
			MessageManager.sendDirectMessageAsync(MessageManager.getMessage("AddCalendar.Auth.Code.Request.Failure.NotOkay", settings), event.getMember().get());

			Logger.getLogger().debug(event.getMember().get(), "Error requesting access token.", "Status code: " + response.code() + " | " + response.message() + " | " + response.body().string(), true, this.getClass());
		}
	} catch (Exception e) {
		//Failed, report issue to dev.
		Logger.getLogger().exception(event.getMember().get(), "Failed to request Google Access Code", e, true, this.getClass());
		Member u = event.getMember().get();
		MessageManager.sendDirectMessageAsync(MessageManager.getMessage("AddCalendar.Auth.Code.Request.Failure.Unknown", settings), u);
	}
}