Create Google Calendar Event from Java Backend from OAuth authenticated Users only

六月ゝ 毕业季﹏ 提交于 2019-12-13 03:15:19

问题


Working on creating a Google Calendar Event on a specific Calendar from backend. Like when someone generate a meeting in a web application, it should be added to the shared google calendar from backend (running using JAVA - Spring boot).

User who is generating event must be an authorized user to do so.

I have followed this docs for initial set up in java.

JAVA docs - initial setup

Dependancies that I've used,

compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
compile 'com.google.apis:google-api-services-calendar:v3-rev305-1.23.0'

Then to create a event I've tried this one,

public String createCalendarEvent() throws GeneralSecurityException, IOException {

    String calendarId = "primary";

    // Build a new authorized API client service.
    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
        .setApplicationName(APPLICATION_NAME)
        .build();

    Event event = new Event()
        .setSummary("Testing Event Creation 1")
        .setLocation("India")
        .setDescription("Testing Event Creation");


    EventDateTime start = new EventDateTime()
        .setDate(new DateTime("2019-02-26"));
    event.setStart(start);

    EventDateTime end = new EventDateTime()
        .setDate(new DateTime("2019-02-28"));
    event.setEnd(end);

    event = service.events().insert(calendarId, event).execute();
    log.debug("Event is created and HTML link is: " + event.getHtmlLink());
    return event.getHtmlLink();
}

getCredential() method is taking care of Authorization as below:

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
    // Load client secrets.
    InputStream in = CalendarEventService.class.getClassLoader().getResourceAsStream(CREDENTIALS_FILE_PATH);
    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(new FileDataStoreFactory(new File(TOKENS_DIRECTORY_PATH)))
        .setAccessType("offline")
        .setApprovalPrompt("force")
        .build();
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
    Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    return credential;
}

The required OAuth ClientID and ClientSecret are located at CREDENTIALS_FILE_PATH. While running it generates AUTH_TOKEN inside TOKENS_DIRECTORY_PATH and create an event in Calendar.

Now subsequent execution execute without any issue If user changes.

I require to create Event in Calendar by the User who is currently logged in (Should be checked for OAuth) and from backend as this one.


回答1:


server to server communication without consent

You need to understand that private user data will always require you to have access. You cant access a users account without that users permission. Oauth2 gives us as developers a way of requesting consent of users to access their data. There is no way around that you will always have to have permission of the user to access their data.

Now if you have a gsuite account you can use a service account and set up domain wide delegation to allow the service account access to all of the users on the domain then it will be able to preform actions on behalf of the users without having to prompt anyone.

If you are only accessing a single account that you personally have control over then you could also use a service account and share the calendar with the service account and then the service account will have access to that account and be able to preform actions upon it.

Credential storage

new FileDataStoreFactory(new File(TOKENS_DIRECTORY_PATH)

denotes where the credentials are stored "user" is a string denoting the user who you are storing credentials for.



来源:https://stackoverflow.com/questions/54886906/create-google-calendar-event-from-java-backend-from-oauth-authenticated-users-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!