I need to access the github graphql API
to get some data about a certain repository. The following curl
command is works fine
curl -i -H "Authorization: bearer myGithubToken" -X POST -d '{"query": "query { repository(owner: "wso2-extensions", name: "identity-inbound-auth-oauth") { object(expression:"83253ce50f189db30c54f13afa5d99021e2d7ece") { ... on Commit { blame(path: "components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpoint.java") { ranges { startingLine endingLine age commit { message url history(first: 2) { edges { node { message url } } } author { name email } } } } } } } }"}' https://api.github.com/graphql
now I need to call the same in Java
as I need to manipulate the outputs. Here is the code that I have tried,
public void callGraphqlApi(){
CloseableHttpClient httpClientForGraphql = null;
CloseableHttpResponse httpResponseFromGraphql= null;
httpClientForGraphql=HttpClients.createDefault();
HttpPost httpPost= new HttpPost("https://api.github.com/graphql");
String query= "query":"query { repository(owner: "wso2-extensions", name:"identity-inbound-auth-oauth") { object(expression: "83253ce50f189db30c54f13afa5d99021e2d7ece") { ... on Commit { blame(path: "components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpoint.java") { ranges { startingLine endingLine age commit { message url history(first: 2) { edges { node { message url } } } author { name email } } } } } } } }";
httpPost.addHeader("Authorization","Bearer myGithubToken");
try {
StringEntity params= new StringEntity(query);
httpPost.addHeader("content-type","application/x-www-form-urlencoded");
httpPost.setEntity(params);
httpResponseFromGraphql= httpClientForGraphql.execute(httpPost);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I debug the code, it showed me this error,
HttpResponseProxy{HTTP/1.1 400 Bad Request [Server: GitHub.com, Date: Fri, 03 Feb 2017 12:14:58 GMT, Content-Type: application/json; charset=utf-8, Content-Length: 89, Status: 400 Bad Request, X-RateLimit-Limit: 200, X-RateLimit-Remaining: 187, X-RateLimit-Reset: 1486125149, X-OAuth-Scopes: repo, user, X-Accepted-OAuth-Scopes: repo, X-GitHub-Media-Type: github.v3; format=json, Access-Control-Expose-Headers: ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, Access-Control-Allow-Origin: *, Content-Security-Policy: default-src 'none', Strict-Transport-Security: max-age=31536000; includeSubdomains; preload, X-Content-Type-Options: nosniff, X-Frame-Options: deny, X-XSS-Protection: 1; mode=block, X-GitHub-Request-Id: CF0A:0EE1:B057F26:EBCB8DF:58947441] ResponseEntityProxy{[Content-Type: application/json; charset=utf-8,Content-Length: 89,Chunked: false]}}
what I am doing wrong? Can you please be kind enough to help me fix this.
Thanks in advance
See Question&Answers more detail:
os