Rework Google Auth and add Supercup

This commit is contained in:
2022-08-05 13:25:26 +02:00
parent f5ef53aefd
commit 63ff6838de
12 changed files with 87 additions and 42 deletions

View File

@@ -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;

View File

@@ -90,7 +90,7 @@ public class TLWMatch extends BaseMatch{
}
}
public TLWMatch(APIFootballMatch apiFootballMatch, int season, int league, int matchday, int matchNo, int status, int koMatch) {
public TLWMatch(APIFootballMatch apiFootballMatch, int season, int league, int matchday, int matchNo, int status, int koMatch, boolean showTable) {
this.season = season;
this.league = league;
this.matchday = matchday;
@@ -105,6 +105,7 @@ public class TLWMatch extends BaseMatch{
this.formulaGuest = "";
this.status = status;
this.koMatch = koMatch;
this.showTable = showTable ? 0 : 1;
this.teamNameHome = apiFootballMatch.getTeamNameHome();
this.teamNameGuest = apiFootballMatch.getTeamNameGuest();
}

View File

@@ -62,7 +62,7 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase {
&& TLWMatchesManagerBase.getDaysDifference(firstDate, matchDateTime) >= 1) {
status = -1;
}
this.TLWMatches.add(new TLWMatch(match, this.season, this.league, TLWMatchday, matchNo, status, this.ko));
this.TLWMatches.add(new TLWMatch(match, this.season, this.league, TLWMatchday, matchNo, status, this.ko, true));
matchdayMatchCounter++;
if(firstDate == null) {
firstDate = matchDateTime;

View File

@@ -40,7 +40,7 @@ public class TLWMatchesCreatorTipperPokal extends TLWMatchesCreatorBase{
int matchday = 1;
for(int matchNo = 1; matchNo < 13; matchNo++) {
for(int matchNo = 1; matchNo <= this.tipperList.size() / 2; matchNo++) {
String homeName = this.tipperList.get(String.valueOf(2 * matchNo - 1)).toString();
String guestName = this.tipperList.get(String.valueOf(2 * matchNo)).toString();
@@ -51,6 +51,7 @@ public class TLWMatchesCreatorTipperPokal extends TLWMatchesCreatorBase{
String matchDatetime = getDeliveryDateForMatchday(matchday);
TLWMatch tlwMatch = new TLWMatch(this.season, this.league, matchday, matchNo, teamIdHome, teamIdGuest, matchDatetime, 1);
tlwMatch.setShowTable(false);
this.TLWMatches.add(tlwMatch);
}
}