Skip to content

Commit

Permalink
Feat: Meet API quickstart
Browse files Browse the repository at this point in the history
  • Loading branch information
sqrrrl committed Mar 7, 2024
1 parent e3102ab commit d492e92
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 0 deletions.
1 change: 1 addition & 0 deletions meet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Additional samples can be found at https://github.com/googleapis/google-cloud-java/tree/main/java-meet/google-cloud-meet
17 changes: 17 additions & 0 deletions meet/quickstart/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'MeetQuickstart'
sourceCompatibility = 11
targetCompatibility = 11
version = '1.0'

repositories {
mavenCentral()
}

dependencies {
implementation 'com.google.cloud:google-cloud-meet:0.3.0'
implementation 'com.google.auth:google-auth-library-oauth2-http:1.19.0'
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.34.1'
}
18 changes: 18 additions & 0 deletions meet/quickstart/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* This settings file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
* In a single project build this file can be empty or even removed.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user guide at https://docs.gradle.org/3.5/userguide/multi_project_builds.html
*/

/*
// To declare projects as part of a multi-project build use the 'include' method
include 'shared'
include 'api'
include 'services:webservice'
*/

rootProject.name = 'quickstart'
174 changes: 174 additions & 0 deletions meet/quickstart/src/main/java/MeetQuickstart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// [START meet_quickstart]

import java.awt.Desktop;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;

import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.apps.meet.v2.CreateSpaceRequest;
import com.google.apps.meet.v2.Space;
import com.google.apps.meet.v2.SpacesServiceClient;
import com.google.apps.meet.v2.SpacesServiceSettings;
import com.google.auth.Credentials;
import com.google.auth.oauth2.ClientId;
import com.google.auth.oauth2.DefaultPKCEProvider;
import com.google.auth.oauth2.TokenStore;
import com.google.auth.oauth2.UserAuthorizer;

/* class to demonstrate use of Drive files list API */
public class MeetQuickstart {
/**
* Directory to store authorization tokens for this application.
*/
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("https://www.googleapis.com/auth/meetings.space.created");

private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

private static final String USER = "default";

// Simple file-based token storage for storing oauth tokens
private static final TokenStore TOKEN_STORE = new TokenStore() {
private Path pathFor(String id) {
return Paths.get(".", TOKENS_DIRECTORY_PATH, id + ".json");
}

@Override
public String load(String id) throws IOException {
if (!Files.exists(pathFor(id))) {
return null;
}
return Files.readString(pathFor(id));
}

@Override
public void store(String id, String token) throws IOException {
Files.createDirectories(Paths.get(".", TOKENS_DIRECTORY_PATH));
Files.writeString(pathFor(id), token);
}

@Override
public void delete(String id) throws IOException {
if (!Files.exists(pathFor(id))) {
return;
}
Files.delete(pathFor(id));
}
};

/**
* Initialize a UserAuthorizer for local authorization.
*
* @param callbackUri
* @return
*/
private static UserAuthorizer getAuthorizer(URI callbackUri) throws IOException {
// Load client secrets.
try (InputStream in = MeetQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH)) {
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}

ClientId clientId = ClientId.fromStream(in);

UserAuthorizer authorizer = UserAuthorizer.newBuilder()
.setClientId(clientId)
.setCallbackUri(callbackUri)
.setScopes(SCOPES)
.setPKCEProvider(new DefaultPKCEProvider() {
// Temporary fix for https://github.com/googleapis/google-auth-library-java/issues/1373
@Override
public String getCodeChallenge() {
return super.getCodeChallenge().split("=")[0];
}
})
.setTokenStore(TOKEN_STORE).build();
return authorizer;
}
}

/**
* Run the OAuth2 flow for local/installed app.
*
* @return An authorized Credential object.
* @throws IOException If the credentials.json file cannot be found.
*/
private static Credentials getCredentials()
throws Exception {

LocalServerReceiver receiver = new LocalServerReceiver.Builder().build();
try {
URI callbackUri = URI.create(receiver.getRedirectUri());
UserAuthorizer authorizer = getAuthorizer(callbackUri);

Credentials credentials = authorizer.getCredentials(USER);
if (credentials != null) {
return credentials;
}

URL authorizationUrl = authorizer.getAuthorizationUrl(USER, "", null);
if (Desktop.isDesktopSupported() &&
Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(authorizationUrl.toURI());
} else {
System.out.printf("Open the following URL to authorize access: %s\n",
authorizationUrl.toExternalForm());
}

String code = receiver.waitForCode();
credentials = authorizer.getAndStoreCredentialsFromCode(USER, code, callbackUri);
return credentials;
} finally {
receiver.stop();
}
}

public static void main(String... args) throws Exception {
// Override default service settings to supply user credentials.
Credentials credentials = getCredentials();
SpacesServiceSettings settings = SpacesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();

try (SpacesServiceClient spacesServiceClient = SpacesServiceClient.create(settings)) {
CreateSpaceRequest request = CreateSpaceRequest.newBuilder()
.setSpace(Space.newBuilder().build())
.build();
Space response = spacesServiceClient.createSpace(request);
System.out.printf("Space created: %s\n", response.getMeetingUri());
} catch (IOException e) {
// TODO(developer): Handle errors
e.printStackTrace();
}
}
}
// [END meet_quickstart]

0 comments on commit d492e92

Please sign in to comment.