Created
September 21, 2017 17:54
-
-
Save salrashid123/63d235a87079743fda6e7a31c77c0009 to your computer and use it in GitHub Desktop.
Google API java client with custom Proxy support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<packaging>jar</packaging> | |
<version>1.0-SNAPSHOT</version> | |
<groupId>com.test.TestApp</groupId> | |
<artifactId>TestApp</artifactId> | |
<properties> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>com.google.apis</groupId> | |
<artifactId>google-api-services-bigquery</artifactId> | |
<version>v2-rev333-1.22.0</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.0.2</version> | |
<configuration> | |
<source>1.7</source> | |
<target>1.7</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.codehaus.mojo</groupId> | |
<artifactId>exec-maven-plugin</artifactId> | |
<version>1.2.1</version> | |
<executions> | |
<execution> | |
<goals> | |
<goal>java</goal> | |
</goals> | |
</execution> | |
</executions> | |
<configuration> | |
<mainClass>com.test.TestApp</mainClass> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Sample demonstrates proxy support for google api clients in java using | |
apachetransport | |
to use, first run a squid proxy server with auth setup: | |
https://github.com/salrashid123/squid_proxy | |
docker run -p 3128:3128 -ti docker.io/salrashid123/squidproxy /bin/bash | |
then when inside the container: | |
/apps/squid/sbin/squid -NsY -f /apps/squid.conf.basicauth | |
*/ | |
package com.test; | |
import java.net.Authenticator; | |
import java.net.PasswordAuthentication; | |
import java.io.IOException; | |
import java.util.*; | |
import java.util.logging.Logger; | |
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; | |
import com.google.api.client.json.jackson2.JacksonFactory; | |
import com.google.api.services.bigquery.*; | |
import com.google.api.services.bigquery.model.*; | |
import java.util.logging.ConsoleHandler; | |
import java.util.logging.Level; | |
import java.util.logging.SimpleFormatter; | |
import org.apache.http.HttpException; | |
import org.apache.http.HttpHost; | |
import org.apache.http.HttpRequestInterceptor; | |
import com.google.api.client.http.HttpRequestInitializer; | |
import com.google.api.client.http.HttpRequest; | |
import com.google.api.client.http.HttpRequestFactory; | |
import com.google.api.client.http.HttpHeaders; | |
import com.google.api.client.json.JsonFactory; | |
import com.google.api.client.http.HttpTransport; | |
import com.google.api.client.http.javanet.NetHttpTransport; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.protocol.HttpContext; | |
import com.google.api.client.http.apache.ApacheHttpTransport; | |
import org.apache.http.auth.UsernamePasswordCredentials; | |
import org.apache.http.conn.params.ConnRoutePNames; | |
import org.apache.http.auth.AuthScope; | |
import org.apache.http.message.BasicHeader; | |
import java.net.Proxy; | |
public class TestApp { | |
public static void main(String[] args) { | |
TestApp tc = new TestApp(); | |
} | |
public TestApp() { | |
try | |
{ | |
ConsoleHandler consoleHandler = new ConsoleHandler(); | |
consoleHandler.setLevel(Level.ALL); | |
consoleHandler.setFormatter(new SimpleFormatter()); | |
Logger logger = Logger.getLogger("com.google.api.client"); | |
logger.setLevel(Level.ALL); | |
logger.addHandler(consoleHandler); | |
Logger lh = Logger.getLogger("httpclient.wire.header"); | |
lh.setLevel(Level.ALL); | |
lh.addHandler(consoleHandler); | |
Logger lc = Logger.getLogger("httpclient.wire.content"); | |
lc.setLevel(Level.ALL); | |
lc.addHandler(consoleHandler); | |
Logger gl = Logger.getLogger("io.grpc"); | |
gl.setLevel(Level.FINE); | |
gl.addHandler(consoleHandler); | |
JacksonFactory jsonFactory = new JacksonFactory(); | |
/* | |
System.setProperty("https.proxyHost", "localhost"); | |
System.setProperty("https.proxyPort", "3128"); | |
Authenticator.setDefault( | |
new Authenticator() { | |
@Override | |
public PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication( | |
"user1", "user1".toCharArray()); | |
} | |
} | |
); | |
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128)); | |
NetHttpTransport mHttpTransport = new NetHttpTransport.Builder().setProxy(proxy).build(); | |
*/ | |
HttpHost proxy = new HttpHost("127.0.0.1",3128); | |
DefaultHttpClient httpClient = new DefaultHttpClient(); | |
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); | |
httpClient.addRequestInterceptor(new HttpRequestInterceptor(){ | |
@Override | |
public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException { | |
if (request.getRequestLine().getMethod().equals("CONNECT")) | |
request.addHeader(new BasicHeader("Proxy-Authorization","Basic dXNlcjE6dXNlcjE=")); | |
} | |
}); | |
ApacheHttpTransport mHttpTransport = new ApacheHttpTransport(httpClient); | |
GoogleCredential credential = GoogleCredential.getApplicationDefault(mHttpTransport,jsonFactory); | |
if (credential.createScopedRequired()) | |
credential = credential.createScoped(Arrays.asList(BigqueryScopes.DEVSTORAGE_READ_ONLY)); | |
Bigquery service = new Bigquery.Builder(mHttpTransport, jsonFactory, credential) | |
.setApplicationName("oauth client") | |
.build(); | |
DatasetList dl = service.datasets().list("mineral-minutia-820").execute(); | |
System.out.println(dl.getDatasets().size()); | |
} | |
catch (Exception ex) { | |
System.out.println("Error: " + ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment