gpt4 book ai didi

java - 如何使用java访问github graphql API

转载 作者:行者123 更新时间:2023-12-02 04:00:47 30 4
gpt4 key购买 nike

我需要访问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 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]}}

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

最佳答案

通过如下更改上面的代码使程序运行。使用 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();
}


}

关于java - 如何使用java访问github graphql API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42024158/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com