请原谅我问这么长的问题,因为我是初学者graphql
。我需要访问github graphql API
获取某个文件的责备详细信息,因为到目前为止还没有责备REST API
可用于github API 版本 3。我可以获得以下输出graphql
运行的查询here
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
}
}
}
}
}
}
}
}
从运行以下命令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
并运行相同的curl
Java中的命令如下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Demo {
public static void main(String[] args) {
String url="https://api.github.com/graphql";
String[] command = {"curl", "-H" ,"Authorization: Bearer myGitHubToken","-H","Accept:application/json","-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 } } } } } } } }\"}" , url};
ProcessBuilder process = new ProcessBuilder(command);
Process p;
try
{
p = process.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
System.out.print(result);
}
catch (IOException e)
{ System.out.print("error");
e.printStackTrace();
}
}
}
有没有其他方法可以得到相同的输出java
不运行curl命令,就像运行一样curl
里面的命令java
这不是一个好的做法(根据我的观点)。提前致谢
使用 httpClient 代码更新
这是我尝试过的代码apache httpClient
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");
String temp="{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 } } } } } } } }";
// String temp="{repository(owner:\"wso2\",name:\"product-is\"){description}}";
try {
StringEntity entity= new StringEntity("{\"query\":\"query "+temp+"\"}");
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();
}
}
但即使有这样的小问题,它也给了我{repository(owner:\"wso2\",name:\"product-is\"){description}}
{“message”:“解析 JSON 时出现问题”,“documentation_url”:“https://developer.github.com/v3"}
但是当这样的简单查询被传递时String temp="{viewer {email login }}";
有用。我的代码有什么问题。请帮忙