【发布时间】:2017-06-23 03:51:55
【问题描述】:
请原谅我的长问题,因为我是graphql 的初学者。我需要访问github graphql API 以获取某个文件的责任详情,因为到目前为止github API version 3 中没有责任REST API。我可以获得以下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
并在 Java 中运行相同的 curl 命令,如下所示
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();
}
}
}
有没有其他方法可以在不运行 curl 命令的情况下在 java 中获得相同的输出,因为在 java 中运行 curl 命令不是一个好习惯(根据我的观点)。提前致谢
使用 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 }}"; 时,它可以工作。我的代码有什么问题。请帮忙
【问题讨论】: