2017-02-03 1 views
0

특정 저장소에 대한 데이터를 얻으려면 github graphql API에 액세스해야합니다. 다음 curl 명령은 내가 출력을 조작하기 위해 필요로하는자바를 사용하여 github graphql API에 액세스하는 방법

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에서 동일하게 호출 할 필요가 잘 작동된다. GitHub.com, 날짜 : 여기에 내가 시도 코드,

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 잘못된 요청 [서버입니다 : Fri, 03 Feb 2017 12:14:58 GMT, Content-Type : application/json; X-RateLimit-Limit : 200, X-RateLimit-Remaining : 187, X-RateLimit-Reset : 1486125149, X-OAuth-Scopes : repo, charset = utf-8, Content- 사용자, X-Accepted-OAuth-Scopes : repo, X-GitHub-Media-Type : github.v3; X-Rate Limit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted- OAuth-Scopes, X-Poll-Interval, 액세스 제어 허용 원본 : *, 콘텐츠 보안 정책 : default-src 'none', Strict-Transport-Security : max-age = 31536000; 서브 도메인; 프리로드, X-Content-Type-Options : nosniff, X- 프레임 옵션 : 거부, X-XSS- 보호 : 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]}}

내가 뭘 잘못하고 있니? 이 문제를 해결할 수 있도록 친절하게 도와 주실 수 있습니까? 미리 감사드립니다.

+0

I 보이는 : 문자열 쿼리로 시작해야 = "{\"쿼리 \ "\"쿼리는 {이 아닌 문자열 쿼리 = "질의 \"\ "쿼리 { – peinearydevelopment

+0

@peinearydevelopment는 검사 그것은 여전히 ​​같은 오류 :( –

답변

0

아래 코드를 변경하여 프로그램을 작동 시켰습니다. 대부분라이브러리를 사용하여 복잡한 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(); 
     } 


    } 
관련 문제