1

I'm trying to develop a Java program to query GitHub GraphQL API. Since I new to Java and the GitHub GraphQL API I thought I would start with a simple query. Once I get this working I can modify the query string, but I can't seem to get the code to work. Can you please help me understand why this code isn't working and what I can do to fix it?

Thanks so much for your help.

I've read through numerous questions here on Stack overflow, but I did not encounter the problem I'm having. I've read descriptions of the Apache http framework, but their examples seemed more complicated that what I'd like to develop. I found some sample code that I shamelessly copied (see below) that seemed straight forward, but I can't get it to work.

I’m running on macOS V10.14.6, Java SE Runtime Environment (build 1.7.0_51-b13), Java HotSpot 64-Bit Server VM (build 24.51-b03, mixed mode), and using IntelliJ IDEA 2019.2 Community Edition (runtime version 11.0.3+12-b304.10 x86_64).

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.json.JSONObject;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

public class httptestcode01
{
    public static void main(String[] args) throws IOException
    {
        // Declare local variables...
        String line, queryString, url;

        url = "https://api.github.com/graphql";
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        client = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);

        httpPost.addHeader("Authorization", "Bearer myToken");
        httpPost.addHeader("Accept", "application/json");

        JSONObject jsonobj = new JSONObject();
        jsonobj.put("query", "{me { firstname } }");

        try {
            StringEntity entity = new StringEntity(jsonobj.toString());
            httpPost.setEntity(entity);
            response = client.execute(httpPost);

            BufferedReader bufReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuilder builder = new StringBuilder();

            while ((line = bufReader.readLine()) != null) {
                builder.append(line);
                builder.append(System.lineSeparator());
            }

            System.out.println(builder);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I compile the code, run it, and get the following output.

/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/java -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/htmlconverter.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/javafx-doclet.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/lib/tools.jar:/Volumes/GoogleDrive/My Drive/soci code base/http test code 01/target/classes:/Users/ciro/.m2/repository/org/apache/httpcomponents/httpclient/4.3.4/httpclient-4.3.4.jar:/Users/ciro/.m2/repository/org/apache/httpcomponents/httpcore/4.3.2/httpcore-4.3.2.jar:/Users/ciro/.m2/repository/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar:/Users/ciro/.m2/repository/commons-codec/commons-codec/1.6/commons-codec-1.6.jar:/Users/ciro/.m2/repository/org/json/json/20190722/json-20190722.jar" httptestcode01
javax.net.ssl.SSLException: Received fatal alert: protocol_version
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
    at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1959)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1077)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:275)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:254)
    at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at httptestcode01.main(httptestcode01.java:36)

Process finished with exit code 0
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • @MarkRotteveel, dank je wel. I looked at the reply you listed as an answer to my question. First I added the command line parameter (-Dhttps.protocols=TLSv1.2) to the IntelliJ IDEA run configuration. When I ran the code it produced the same error message. Then I added the suggested command (java.lang.System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");) to my code, but got the same result. – Ciro Pinto-Coelho Aug 17 '19 at 13:01
  • Then maybe you should try and use Java 8 instead. – Mark Rotteveel Aug 17 '19 at 13:07
  • According to the Java control panel app, my Os has "Version 8 Update 221 (build 1.8.0_221-b11)" installed. A `java -version` command returns `Java(TM) SE Runtime Environment (build 1.7.0_51-b13)`. What am I missing to enable Java 8? I apologize for my confusion, this is all new to me. – Ciro Pinto-Coelho Aug 17 '19 at 13:36
  • @MarkRotteveel I was able to set the macOS and IntelliJ IDEA IDE parameters to use the 1.8 JDK. That eliminated the error...now on to authenticating to the GraphQL API. Dank je wel for your help, much appreciated! – Ciro Pinto-Coelho Aug 17 '19 at 14:58

1 Answers1

2

While "javax.net.ssl.SSLException: Received fatal alert: protocol_version" explains that Java 1.6 and 1.7 default is obsoleted TLS1.0, it does not do so in the context of https://api.github.com.

Since February 2017, GitHub has discontinued support for weak cryptographic standards, including TLSv1.

As noted in this bug report:

  • Oracle JDK 1.7u80 is the last Oracle public release
  • If you used 1.7u131 (from Oracle paying support or OpenJDK), you would not need the system property: see release notes:
    "Add TLS v1.1 and v1.2 to the client list of default-enabled protocols"

In your case, try at lest to add to your java command:

-Dhttps.protocols=TLSv1.2

Or upgrade to a more recent Java version, especially considering jdk 1.7 is eol -- end of life -- since May 2015.
Even through commercial support, according to the Java roadmap, Premier support has ended last month (July 2019), and extended support will end in July 2022.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Why does Java 1.7 defaulting to TLSv1 not explain the problem? Your solution to enable TLSv1.2 is the same as provided by some of the answers on the linked question. – Mark Rotteveel Aug 17 '19 at 06:50
  • @MarkRotteveel I agree. I wanted to add more GitHub-specific context, and explore the state of support of JDK 1.7. Those two elements are the added values of this answer. – VonC Aug 17 '19 at 06:58
  • @VonC merci beaucoup. I added -Dhttps.protocols=TLSv1.2 to the IntelliJ run configuration, recompiled the code, ran it and got the same results.
    I tried to update the JRE from [Oracle for macOS] (https://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html), but a 'java -version' command in Terminal returns "JRE 1.7.0_51-b13." I'm not sure how to proceed. I appreciate any suggestions you might have.
    – Ciro Pinto-Coelho Aug 17 '19 at 13:31
  • @VonC, I was able to set the macOS and IntelliJ IDEA IDE parameters to use the 1.8 JDK. That eliminated the error...now on to authenticating to the GraphQL API. Merci beaucoup for your help, much appreciated! – Ciro Pinto-Coelho Aug 17 '19 at 14:59
  • @CiroPinto-Coelho Great, well done! – VonC Aug 17 '19 at 19:02