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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally we would make a seperate sample (including a seperate class, test etc) if we were going to add a sample like this and not add it into the existing sample for another use case.

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