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

@@ -18,12 +18,12 @@ repositories {
}
dependencies {
implementation 'mysql:mysql-connector-java:8.0.28'
implementation 'mysql:mysql-connector-java:8.0.29'
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
implementation 'com.google.code.gson:gson:2.9.0'
implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.896')
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.173'
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.239'
implementation 'com.google.api-client:google-api-client:1.33.2'
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.33.1'
implementation 'com.google.apis:google-api-services-calendar:v3-rev20211026-1.32.1'

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);
}
}

View File

@@ -0,0 +1,12 @@
{
"type": "service_account",
"project_id": "tippliga",
"private_key_id": "4e0def9ef4474114744a1fcaffae9e75689a8909",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDaJeiT0eCLSq6l\nfjvNxE9EnHtFxvWdJOx2CsIXvBJKA7u1b95uHSNQmmunJPMstMLvTRYYYVpJWUm0\njqLd4VGUDqjXuWcNQpn62TFLSGQs8bTwIYoYsAnTWC+kuG5iSaKKzw9UiKFx0Mxv\nLAm5CjqYb87E2pz+7ihCYfaMkpppwPhz8bUbQ+iHdo85cQBcZeVpjuL+W4KT89yV\nZxsV4dGJNabLLEznA1WyhS+pGz+3N9edfANyREthrxm1cyytd3omQ/pfIl7KpGlU\nIq4eDMzlPftn+Nq9S7UfTXzxcv9Ez2cdlGMSSSbpjQBYXNEewnzXutFqfK9h2STD\nM0XaqGh9AgMBAAECggEAAzMrOWAmI8zsUmcPYANG6WMgp6NVhcV46zEP/iTroVRg\nwr+r8bJWOJj+IjCgq4hQ09a3KyYGtlXwRrrH5HPyQyTAnqLjZtZ/3lzblm2TYJmJ\nwzwcbO1+skJCAF2Dxmxh35jgTafQIQJNhxheqtrwBStVQhUIIbosPBQvkven+tUv\n4lRn494VXNxWZSGevL+3iJoNtJbjjQ8FVLafx6/vKP4mShB0UxKHqSBRtzZ5g4/n\nB8d4Vw4Ma0eEBUMDD9vOqreMdKCUoMnZIKuQwWeyiug77mBxgfmCRnsefOdyEKne\nRvalPBKEpD2PH/iud+qkuNNITFCr0I1iF6f4NElciQKBgQD/q68TvMOUE2tlgdU6\n6o6d7b+Sm4yp19f1euR0fe7+5as6Pz4KWy0zwSXAczVNDXLq44hQ4eeJMALwKvQ+\nXqCrtjc3NkVtiPXIjnqEQ/t56WBZtsrLxlNAE9yxHQPEmyRMKjGOJTRpoNZzGBsS\naEqTiKinO7AY5xOWzXz+HVLDRQKBgQDabdmrKLJaHhboPBa5B7HJ8WCL5P5+97P6\n4WuDT7vQvyJqb3KhltdRAgMszFXsfYwyeidncQ3fxUlmMWtWlHROfjhyWQZ0jIJg\nwpTAvNqQ3HuXM2fRvM7d6Z69xE2RxVpRaAbvGUHvNohAW7Vwmoi/GV9NR6irV/ny\n1suS6SIH2QKBgHnJXEJ4nmlAvt3BdML1zC2qfKDne5/D81k5yu06t8evu1qpmXHx\ncJzAmq0LtA4aI+2DqQFuOo580kl5DJNRHBPvXmnsbjH5FasNQ4sRnw65fm39xqg+\ntpVFYND1U0Ap7qZhSu29BzRxqvV0PCEOkMeqRzwyEHYsTIZ/J9UcPrpJAoGAcNQ/\nlN8vycczNAEeB7QGzXbG1VyTbYiMSrK4c5rzPHAO2gX5o6ikTm8IrtJaLA/qJITe\n59sJPKbPNF+gv5dtwMIIbGC6KegOzOaWKj0jdaK+oKroHPo3rX+pBQNqwjq8XcKr\nmkRkv6lelkgJIEJjEruLdALpCKEoNp1niNquPnkCgYAVAkaF/8xYQ4UaBuJEJPOW\nDNXAmRrvaazU8GTv70t03VHKjhcv7b83L+n8yU3VuaqaO3bgyGIELJ2VRzVn16EH\nkUwh1W2fB1ZAmMK6V5JB7b8LCPdpCJSbQvNZAOZj4aodoNtGKM0j36rTSZJi2Rdz\nqtOSw0pEvALI62ufuNRUzA==\n-----END PRIVATE KEY-----\n",
"client_email": "tlw-707@tippliga.iam.gserviceaccount.com",
"client_id": "101590566618120833710",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/tlw-707%40tippliga.iam.gserviceaccount.com"
}

View File

@@ -51,7 +51,7 @@ public class TippligaGoogleEventManagerTest {
public void updateEventTest() {
List<Event> allEvents = getAllEvents(2023);
Event event = findEvent(allEvents, 2023, 1, 1, 1);
TLWMatchday matchday = new TLWMatchday(2023, 1, 1, 0, "2022-08-05 20:30:00", "", "", "1. Spieltag", 12);
TLWMatchday matchday = new TLWMatchday(2023, 1, 1, 0, "2022-08-06 20:30:00", "", "", "1. Spieltag", 12);
updateEvent(event, matchday, matchday.getDeliveryDate(), 1);
assertEquals(event.size(), 18);
}
@@ -67,7 +67,7 @@ public class TippligaGoogleEventManagerTest {
@Test
public void createOrUpdateEventTest() {
TLWMatchday matchday = new TLWMatchday(2023, 1, 1, 0, "2022-08-06 20:30:00", "", "", "1. Spieltag", 12);
TLWMatchday matchday = new TLWMatchday(2023, 1, 1, 0, "2022-08-05 20:30:00", "", "", "1. Spieltag", 12);
createOrUpdateEvent(matchday, matchday.getDeliveryDate(), 1);
}

View File

@@ -76,6 +76,22 @@ public class TLWMatchdaysCreatorTest {
System.out.println(sql);
}
@Test
public void getMatchdaysSupercup45SqlTest() {
TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(season, 45, "Supercup");
String sql = matchdaysCreator.getMatchdaysSQL();
System.out.println(sql);
}
@Test
public void getMatchdaysSupercup95SqlTest() {
TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(season, 95, "Supercup");
String sql = matchdaysCreator.getMatchdaysSQL();
System.out.println(sql);
}
@Test
public void getMatchdaysEMTippspielSqlTest() {

View File

@@ -41,6 +41,13 @@ public class TLWMatchesCreatorFootballTest {
System.out.println(sql);
}
@Test
public void getMatchesSupercupSqlTest() {
TLWMatchesCreatorFootball creator = new TLWMatchesCreatorFootball(season, 45, "Supercup");
String sql = creator.getSQLInsertString();
System.out.println(sql);
}
@Test
public void getMatchesEMTippspielSqlTest() {
TLWMatchesCreatorFootball creator = new TLWMatchesCreatorFootball(2021, 49, "EM-Tippspiel");

View File

@@ -15,4 +15,14 @@ public class TLWMatchesCreatorTipperPokalTest {
String sql = creator.getSQLInsertString();
System.out.println(sql);
}
@Test
public void getMatchesSupercupTest() {
TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(season, 45, "Supercup");
TLWMatchesCreatorTipperPokal creator = new TLWMatchesCreatorTipperPokal(season, 95, "Supercup Tipper", matchdaysCreator.getMatchdays());
String sql = creator.getSQLInsertString();
System.out.println(sql);
}
}

View File

@@ -41,6 +41,16 @@ public class TLWTeamsCreatorTest {
System.out.println(sql);
}
@Test
public void getTeamsTippligaSupercupTest() {
TLWMatchesCreatorFootball matchesCreator = new TLWMatchesCreatorFootball(season, 45, "Supercup");
ArrayList<TLWMatch> matches = matchesCreator.getMatches();
TLWTeamsCreator teamCreator = new TLWTeamsCreator(season, 45, matches);
String sql = teamCreator.getSql();
System.out.println(sql);
}
@Test
public void getTeamsTippligaEMTippspielTest() {
TLWMatchesCreatorFootball matchesCreator = new TLWMatchesCreatorFootball(2021, 49, "EM-Tippspiel");
@@ -89,4 +99,18 @@ public class TLWTeamsCreatorTest {
String sql = teamCreator.getSql();
System.out.println(sql);
}
@Test
public void getTeamsTipperSupercupTest() {
int leagueId = 45;
TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(season, leagueId, "Supercup");
TLWMatchesCreatorTipperPokal creator = new TLWMatchesCreatorTipperPokal(season, leagueId + 50, "Supercup Tipper", matchdaysCreator.getMatchdays());
ArrayList<TLWMatch> matches = creator.getMatches();
TLWTeamsCreator teamCreator = new TLWTeamsCreator(season, leagueId + 50, matches);
String sql = teamCreator.getSql();
System.out.println(sql);
}
}

Binary file not shown.