2017-02-03 64 views
0

我需要訪問github graphql API才能獲取有關某個存儲庫的某些數據。下面curl命令工作正常如何使用java訪問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一樣,我需要處理的輸出。這裏是我試過的代碼,

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 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,用戶,X-Accepted-OAuth-Scopes:回購,X-GitHub-Media-Type:github.v3;格式= 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;預加載,X-Content-Type-Options:nosniff,X-Frame-Options:拒絕,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]}}

我在做什麼錯?你能不能幫我解決這個問題。 在此先感謝

+0

我看起來像你的JSON是無效的:如果有查詢字符串開始= 「{\」 查詢\ 「:\」 查詢{,不串查詢= 「查詢\」:\「查詢{ – peinearydevelopment

+0

@peinearydevelopment經過它仍然是一樣的錯誤:( –

回答

0

通過更改上面的代碼得到程序工作如下。使用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(); 
     } 


    }