2017-02-03 6 views
0

zugreifen Ich brauche Zugriff auf die github graphql API, um einige Daten über ein bestimmtes Repository zu erhalten. Der folgende curl Befehl funktioniertWie Github Graphql API mit Java

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 

jetzt ich das gleiche in Java anrufen müssen, wie ich die Ausgänge manipulieren müssen. Hier ist der Code, den ich versucht habe,

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(); 
    } 

} 

Wenn ich den Code debuggen, es zeigte mir diesen Fehler,

HttpResponseProxy {HTTP/1.1 400 Bad Request [Server: GitHub.com, Datum : Fr, 03 Feb 2017 12:14:58 GMT, Content-Type: application/json; charset = utf-8, Content-Length: 89, Status: 400 Ungültige Anfrage, X-RateLimit-Limit: 200, X-RateLimit-Verbleibend: 187, X-RateLimit-Reset: 1486125149, X-OAuth-Scopes: Repo, Benutzer, X-Accepted-OAuth-Bereiche: Repo, X-GitHub-Media-Typ: github.v3; format = json, Zugriffssteuerungs-Expose-Header: ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Verbleibend, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted- OAuth-Bereiche, X-Abfrage-Intervall, Zugriffssteuerung-Zulassen-Ursprung: *, Inhalt-Sicherheitsrichtlinie: Standard-src 'keine', Strict-Transport-Sicherheit: max-age = 31536000; IncludeSubdomains; preload, X-Content-Type-Optionen: nosniff, X-Frame-Optionen: verweigern, X-XSS-Protection: 1; mode = Block, X-GitHub-Anfrage-ID: CF0A: 0EE1: B057F26: EBCB8DF: 58947441] ResponseEntityProxy {[Inhaltstyp: application/json; charset = utf-8, Content-Length: 89, Chunked: false]}}

was mache ich falsch? Kannst du bitte so freundlich sein, mir dabei zu helfen, das zu beheben? Vielen Dank im Voraus

+0

I sieht aus wie Ihre JSON ungültig ist: Sollte mit String-Abfrage starten = "{\" query \ ": \" query {nicht String query = "query \": \ query "{ – peinearydevelopment

+0

@peinearydevelopment Checked es ist immer noch der gleiche Fehler :( –

Antwort

0

Haben Sie das Programm funktioniert, indem Sie den obigen Code wie folgt ändern. Und es ist eine gute Praxis, eine JSON Bibliothek zu verwenden, um komplexes JSON wie oben anders als manuell zu schaffen es zu schaffen, wie die meiste Zeit, kann manuelles Erschaffen eines Komplexes JSON zu viele Schwierigkeiten führen.

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(); 
     } 


    }