2

저지에서 HTTP 게시 요청을해야하는 Java 프로젝트에 Microsoft Translator를 사용하고 있습니다. 현재 코드로 인해 HTTP 400 오류 - 잘못된 요청이 발생합니다. 나는 HTTP에 아주 새롭다 - 누군가가 올바른 방향으로 나를 가리킬 수 있을까?Microsoft Translator와 Java/Jersey/REST 사용

마이크로 소프트 액세스 토큰 지침 : http://msdn.microsoft.com/en-us/library/hh454950

package com.mkyong.client; 

    import com.sun.jersey.api.client.Client; 
    import java.net.*; 

    import javax.ws.rs.core.EntityTag; 
    import javax.ws.rs.core.MediaType; 

    import com.sun.jersey.api.client.ClientResponse; 
    import com.sun.jersey.api.client.WebResource; 

    public class JerseyClientPost { 

public static void main(String[] args) { 

    try { 

     Client client = Client.create(); 

     WebResource webResource = client 
        .resource("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"); 
     //Paramaters for Access Token 
     //String inpu2t = "{\"Tyler_Kevin\",\"VcwDLMGuFMLnUgql...\",\"http://api.microsofttranslator.com\",\"client_credentials\"}"; 

     String input = "{\"client_id\":\"Tyler_Kevin\",\"client_secret\":\"VcwDLMGuFMLnUgqldjrfj....",\"scope\":\"http://api.microsofttranslator.com\",\"grant_type\":\"client_credentials\"}"; 

     //Send HTTP POST request to Microsoft Server 
     ClientResponse response = webResource.type("application/json") 
       .post(ClientResponse.class, input); 


     //If request is not successful, throw error 
     if (response.getStatus() != 201) { 
      throw new RuntimeException("Failed =/ : HTTP error code : " 
        + response.getStatus()); 
     } 

     //Display server response 
     System.out.println("Output from Server .... \n"); 
     String output = response.getEntity(String.class); 
     System.out.println(output); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
} 

답변

1

당신은 응용 프로그램/x-www-form-urlencoded를 토큰 서비스에 데이터를 보낼 때를 사용해야합니다. 따라서 다음을 시도해 보겠습니다.

com.sun.jersey.api.representation.Form input = new Form(); 
input.add("client_id", "Tyler_Kevin"); 
input.add("client_secret", "VcwDLMGuFMLnUgqldj....."); 
input.add("scope", "http://api.microsofttranslator.com"); 
input.add("grant_type", "client_credentials"); 

// send HTTP POST 
ClientResponse response = webResource.type("application/x-www-form-urlencoded") 
     .post(ClientResponse.class, input); 
+0

불행히도, 나는 여전히 400 오류가 발생합니다. –

+0

업데이트 - "application"이 webResource.type ("applicatin"...) 행에 오타되었습니다. 그것을 수정 완벽하게 작동합니다. 도와 줘서 고마워! –

관련 문제