Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example of how to change the oauth endpoint #1972

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,21 +17,44 @@
package com.example.storage.bucket;

// [START storage_set_client_endpoint]

import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

public class SetClientEndpoint {

public static ServiceAccountCredentials setOAuthEndpoint(
String oauthEndpoint, String serviceAccountPath) throws IOException, URISyntaxException {
// The oauth endpoint you wish to target
// String oauthEndpoint = "https://oauth2.googleapis.com/token";

ServiceAccountCredentials credentials =
ServiceAccountCredentials.fromStream(new FileInputStream(serviceAccountPath));
credentials = credentials.toBuilder().setTokenServerUri(new URL(oauthEndpoint).toURI()).build();
return credentials;
}

public static void setClientEndpoint(String projectId, String endpoint) {
// The ID of your GCP project
// String projectId = "your-project-id";

// The endpoint you wish to target
// String endpoint = "https://storage.googleapis.com"

// You might want to change the oauth2 endpoint as well
// ServiceAccountCredentials credentials = setOAuthEndpoint(oauthEndpoint, serviceAccountPath);

Storage storage =
StorageOptions.newBuilder().setProjectId(projectId).setHost(endpoint).build().getService();
StorageOptions.newBuilder()
.setProjectId(projectId)
.setHost(endpoint)
// .setCredentials(credentials)
.build()
.getService();

System.out.println(
"Storage Client initialized with endpoint " + storage.getOptions().getHost());
Expand Down