Rework Google Auth and add Supercup
This commit is contained in:
@@ -1,62 +1,36 @@
|
||||
package de.jeyp91.googlecalendar;
|
||||
|
||||
import com.google.api.client.auth.oauth2.Credential;
|
||||
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
|
||||
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
|
||||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
|
||||
import com.google.api.client.http.javanet.NetHttpTransport;
|
||||
import com.google.api.client.json.JsonFactory;
|
||||
import com.google.api.client.json.gson.GsonFactory;
|
||||
import com.google.api.client.util.store.FileDataStoreFactory;
|
||||
import com.google.api.services.calendar.CalendarScopes;
|
||||
import com.google.api.services.calendar.Calendar;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.io.*;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class GoogleCalendarConnector {
|
||||
private static final String APPLICATION_NAME = "Google Calendar API Java Quickstart";
|
||||
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
|
||||
private static final String TOKENS_DIRECTORY_PATH = "tokens";
|
||||
|
||||
/**
|
||||
* Global instance of the scopes required by this quickstart.
|
||||
* If modifying these scopes, delete your previously saved tokens/ folder.
|
||||
*/
|
||||
private static final List<String> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR);
|
||||
private static final String CREDENTIALS_FILE_PATH = "/Google_Credentials.json";
|
||||
private static final String SERVICE_CREDENTIALS_FILE_PATH = "/tippliga-4e0def9ef447.json";
|
||||
|
||||
/**
|
||||
* Creates an authorized Credential object.
|
||||
* @param HTTP_TRANSPORT The network HTTP Transport.
|
||||
* @return An authorized Credential object.
|
||||
* @throws IOException If the Google_Credentials.json file cannot be found.
|
||||
*/
|
||||
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
|
||||
// Load client secrets.
|
||||
InputStream in = GoogleCalendarConnector.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
|
||||
if (in == null) {
|
||||
throw new FileNotFoundException("Resource not found: " + 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 java.io.File(TOKENS_DIRECTORY_PATH)))
|
||||
.setAccessType("offline")
|
||||
.build();
|
||||
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
|
||||
Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
|
||||
//returns an authorized Credential object.
|
||||
return credential;
|
||||
private static GoogleCredential getCredentials() throws IOException {
|
||||
InputStream in = GoogleCalendarConnector.class.getResourceAsStream(SERVICE_CREDENTIALS_FILE_PATH);
|
||||
return GoogleCredential.fromStream(in)
|
||||
.createScoped(Collections.singleton(CalendarScopes.CALENDAR));
|
||||
}
|
||||
|
||||
public static com.google.api.services.calendar.Calendar getSerivce() {
|
||||
@@ -66,11 +40,11 @@ public class GoogleCalendarConnector {
|
||||
final NetHttpTransport HTTP_TRANSPORT;
|
||||
try {
|
||||
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
|
||||
service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
|
||||
service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials())
|
||||
.setApplicationName(APPLICATION_NAME)
|
||||
.build();
|
||||
|
||||
} catch (GeneralSecurityException | IOException e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return service;
|
||||
|
||||
Reference in New Issue
Block a user