Hello,
import java.io.InputStream;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.nio.channels.Channels;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
import com.google.auth.http.HttpTransportFactory;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
private static void queryBucket(InputStream accessCredentialJson) {
GoogleCredentials googleCredentials = GoogleCredentials.fromStream(accessCredentialJson, getHttpTransportFactory());
Storage storage = StorageOptions.newBuilder().setCredentials(googleCredentials).build().getService();
Bucket gcpBucketObj = storage.get("gcpBucket"); // --> Throws 407 Proxy Auth Reqd. Error
}
public HttpTransportFactory getHttpTransportFactory() {
HttpHost proxyHostDetails = new HttpHost(PROXY_HOST, PROXY_PORT);
HttpRoutePlanner httpRoutePlanner = new DefaultProxyRoutePlanner(proxyHostDetails);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
new AuthScope(proxyHostDetails.getHostName(), proxyHostDetails.getPort()),
new UsernamePasswordCredentials(PROXY_USERNAME, PROXY_PASSWORD)
);
HttpClient httpClient = ApacheHttpTransport.newDefaultHttpClientBuilder()
.setRoutePlanner(httpRoutePlanner)
.setProxyAuthenticationStrategy(ProxyAuthenticationStrategy.INSTANCE)
.setDefaultCredentialsProvider(credentialsProvider)
.build();
final HttpTransport httpTransport = new ApacheHttpTransport(httpClient);
return new HttpTransportFactory() {
@Override
public HttpTransport create() {
return httpTransport;
}
};
}
System.setProperty("https.proxyHost", PROXY_HOST);
System.setProperty("https.proxyPort", PROXY_PORT);
com.google.cloud.storage.StorageException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required"
at com.google.cloud.storage.StorageException.translate(StorageException.java:170)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:313)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.get(HttpStorageRpc.java:504)
at com.google.cloud.storage.StorageImpl.lambda$internalBucketGet$65(StorageImpl.java:1589)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:103)
at com.google.cloud.RetryHelper.run(RetryHelper.java:76)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:50)
at com.google.cloud.storage.Retrying.run(Retrying.java:65)
at com.google.cloud.storage.StorageImpl.run(StorageImpl.java:1514)
at com.google.cloud.storage.StorageImpl.internalBucketGet(StorageImpl.java:1587)
at com.google.cloud.storage.StorageImpl.get(StorageImpl.java:316)
at com.application.DataHandler.getData(DataHandler.java:310)
Hello @kumaran1203,
Welcome to the Google Cloud Community!
Please check your proxy credentials to make sure the username and password are correct. The error message indicates that there is an issue with proxy authentication when accessing Google Cloud Storage.
Hello juliadeanne, thanks for the response.
I have ensured that the correct proxy credentials are passed to the Google client.
I am also able to connect to Google servers from our server directly by attaching the proxy credentials in curl command. The 407 error is thrown only when I try using Google Cloud SDK.
Please let me know what i"m doing wrong.
I have verified that my proxy credentials are correctly passed.
When I tried connecting google servers from my machine directly using curl request with proxy credentials, the connection was established, meaning proxy credentials are correct. Only when request is made via Google Cloud SDK, i'm facing this error.
Please help me on what I'm doing wrong in the code.
User | Count |
---|---|
7 | |
2 | |
1 | |
1 | |
1 |