4

appengine で Twilio の Java クライアント ライブラリを使用しようとしています。そのままでは実行されないようで、クライアント ライブラリに変更を加える必要があります。次の変更を加えましたが、機能しません。

public TwilioRestClient(String accountSid, String authToken, String endpoint) {

    validateAccountSid(accountSid);
    validateAuthToken(authToken);

    this.accountSid = accountSid;
    this.authToken = authToken;

    if ((endpoint != null) && (!endpoint.equals(""))) {
        this.endpoint = endpoint;
    }



/* origial code
 *  ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager();
 *      connMgr.setDefaultMaxPerRoute(10);
 *      this.httpclient = new DefaultHttpClient(connMgr);
 */

 //my changes start HERE
    SchemeRegistry schemeRegistry = new SchemeRegistry(); 
    schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); 
    BasicHttpParams params = new BasicHttpParams(); 

    ThreadSafeClientConnManager connMgr = new  ThreadSafeClientConnManager(params,schemeRegistry);
    //my changes END HERE

    this.httpclient = new DefaultHttpClient();

    httpclient.getParams().setParameter("http.protocol.version",
            HttpVersion.HTTP_1_1);
    httpclient.getParams().setParameter("http.socket.timeout",
            new Integer(CONNECTION_TIMEOUT));
    httpclient.getParams().setParameter("http.connection.timeout",
            new Integer(CONNECTION_TIMEOUT));
    httpclient.getParams().setParameter("http.protocol.content-charset",
            "UTF-8");

    this.authAccount = new Account(this);
    this.authAccount.setSid(this.accountSid);
    this.authAccount.setAuthToken(this.authToken);

}

これによりコンパイルして実行できますが、次の例外が発生します: 原因: org.apache.http.HttpException: スキーム 'https' が登録されていません。リクエストが行われたとき。リクエストは、この (変更されていない) コードにあります。

public TwilioRestResponse request(String path, String method,
        Map<String, String> vars) throws TwilioRestException {

    HttpUriRequest request = setupRequest(path, method, vars);

    HttpResponse response;
    try {
        response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();

        Header[] contentTypeHeaders = response.getHeaders("Content-Type");
        String responseBody = "";

        if (entity != null) {
            responseBody = EntityUtils.toString(entity);
        }

        StatusLine status = response.getStatusLine();
        int statusCode = status.getStatusCode();

        TwilioRestResponse restResponse = new TwilioRestResponse(request
                .getURI().toString(), responseBody, statusCode);

        // For now we only set the first content type seen
        for (Header h : contentTypeHeaders) {
            restResponse.setContentType(h.getValue());
            break;
        }

        return restResponse;

    } catch (ClientProtocolException e1) {
        throw new RuntimeException(e1);
    } catch (IOException e1) {
        throw new RuntimeException(e1);
    }
}

twilio で Java 用の appengine を使用した人はいますか? 基本認証を使用した XML または JSON を使用した単なる RESTful 要求であることはわかっています。私はクライアント ライブラリを使用し、自分でリクエストのコーディングに時間を費やさないようにしたいと考えていました...何かアイデアはありますか?

4

2 に答える 2

3

最新の Twilio Java クライアント ライブラリは、appengine (Apache HttpClient) とうまく連携しない外部ライブラリに依存しています。Twilio のオリジナルの Java クライアント コードは、java.net.HttpURLConnection を使用しただけで、外部依存関係はありませんでした。

この古い Twilio Java クライアント コードをtwilio4jに追加しました (いくつかのバグ修正を行いました)。制作に使用しています。

TODO: appengine に適した公式の twilio-java プロジェクトにパッチを提供する必要があります。

于 2012-04-30T23:49:57.377 に答える
1

基本的な問題は、Twilio-Java がTwilio のサーバーへの接続にApache HttpClientを使用しており、そのままでは Google AppEngine と互換性がないことです。

Google AppEngine で Apache HttpClientを使用することができます。ソース コードを取得し、それらの指示に従って Twilio-Java クライアント ライブラリを変更する必要があります。

于 2012-04-30T04:36:07.957 に答える