2

私は、JerseyがHTTPPOSTリクエストを行う必要があるJavaプロジェクトにMicrosoftTranslatorを使用しています。私の現在のコードでは、HTTP400エラー(不正なリクエスト)が発生します。私はHTTPに非常に慣れていません-誰かが私を正しい方向に向けることができますか?

Microsoftアクセストークンの手順: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();
    }
}
}
4

1 に答える 1

1

トークンサービスにデータを送信するときは、application/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);
于 2012-06-14T12:37:53.677 に答える