繁体   English   中英

如何使用Java访问github graphql API

[英]how to access github graphql API using java

我需要访问github graphql API以获取有关某个存储库的一些数据。 以下curl命令正常工作

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

现在,我需要在Java调用与操作输出相同的名称。 这是我尝试过的代码,

  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();
    }

}

当我调试代码时,它向我显示此错误,

HttpResponseProxy {HTTP / 1.1 400错误的请求[服务器:GitHub.com,日期:2017年2月3日,星期五,格林尼治标准时间12:14:58,内容类型:application / json; charset = utf-8,内容长度:89,状态:400错误的请求,X-RateLimit-Limit:200,X-RateLimit-Remaining:187,X-RateLimit-Reset:1486125149,X-OAuth-Scopes:repo,用户,X-Accepted-OAuth-Scopes:回购,X-GitHub-Media-Type:github.v3; format = json,Access-Control-Expose-Header:ETag,链接,X-GitHub-OTP,X-RateLimit-Limit,X-RateLimit-Remaining,X-RateLimit-Reset,X-OAuth-Scopes,X-Accepted- OAuth范围,X轮询间隔,访问控制允许来源:*,内容安全策略:default-src'none',严格传输安全性:max-age = 31536000; includeSubdomains; 预加载,X-Content-Type-Options:nosniff,X-Frame-Options:deny,X-XSS-Protection:1; 模式=块,X-GitHub-Request-Id:CF0A:0EE1:B057F26:EBCB8DF:58947441] ResponseEntityProxy {[Content-Type:application / json; charset = utf-8,内容长度:89,已压缩:false]}}

我做错了什么? 您能帮我解决这个问题吗? 提前致谢

通过更改以下代码使程序正常工作。 与使用JSON库创建复杂的JSON一样,这是一个好习惯,而不是像大多数情况下手动创建那样,手动创建复杂的JSON可能会带来很多麻烦。

import org.json.JSONObject;

public void callingGraph(){
        CloseableHttpClient client= null;
        CloseableHttpResponse response= null;

        client= HttpClients.createDefault();
        HttpPost httpPost= new HttpPost("https://api.github.com/graphql");

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

        JSONObject jsonObj = new JSONObject();     
        jsonobj.put("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 } } } } } } } }");

        try {
            StringEntity entity= new StringEntity(jsonObj.toString());

            httpPost.setEntity(entity);
            response= client.execute(httpPost);

        }

        catch(UnsupportedEncodingException e){
            e.printStackTrace();
        }
        catch(ClientProtocolException e){
            e.printStackTrace();
        }
        catch(IOException e){
            e.printStackTrace();
        }

        try{
            BufferedReader reader= new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line= null;
            StringBuilder builder= new StringBuilder();
            while((line=reader.readLine())!= null){

                builder.append(line);

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


    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM