如何在Google AppEngine中使用Twilio和GWT(Java) [英] How to use Twilio with GWT in Google AppEngine (Java)

查看:159
本文介绍了如何在Google AppEngine中使用Twilio和GWT(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的GWT应用程序中使用Twilio的官方Java库来发送短信。



以下是我在应用程序中使用的Twilio代码:

 公共类TwilioSMS {
/ **常量ACCOUNT_SID。 * /
public static final String ACCOUNT_SID =xxxxxxxxxxxxxxxxxxxxxxxxxx;
public static final String AUTH_TOKEN =xxxxxxxxxxxxxxxxxxxxxxxxx;


//创建一个休息客户
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID,AUTH_TOKEN);


/ **
*主要方法。
*
* @param args
*参数
* @throws TwilioRestException
* twilio rest异常
* /



public String sendMessage(String _to,String _message)throws TwilioRestException
{

//获取主账户(我们用来验证客户端的一个
Account mainAccount = client.getAccount();

//发送短信
SmsFactory smsFactory = mainAccount.getSmsFactory();
Map< String,String> smsParams =新的HashMap< String,String>();
smsParams.put(To,_to); //替换为有效的电话号码
smsParams.put(From,(646)755 -7665); //用您的账户中有效的电话号码替换
smsParams.put(Body,_message);
smsFactory.create(smsParams);



//向api发出原始请求
TwilioRestResponse resp = client.request(/ 2010-04-01 / Accounts,GET,
null);
if(!resp.isError()){
return resp.getResponseText();
}
else
{
return无法发送消息。;
}

}

}

在GAE中运行代码时,出现以下异常:

  java.lang.NoClassDefFoundError:javax.net。 ssl.KeyManagerFactory是一个受限制的类。有关更多详细信息,请参阅Google App Engine开发人员指南。 

我意识到有一个gwt-twilio http://code.google.com/p/gwt-twilio/ ,但这是twilio客户端的封装(它不处理发送短信)



在GAE + GWT中使用twilio发送短信的任何示例都很有用!

谢谢

Kun

解决方案

我知道这是旧的,但如果可以,我想分享一些信息。截至2014年1月,如果您愿意,可以在App Engine上使用Twilio助手库。 Twilio Java库的基础HTTP客户端实现已被修改为在App Engine上运行。

另外,为了清楚起见,您不应该尝试在GWT客户端使用Twilio助手库。 Twilio助手库仅在代码在服务器上执行时才可用。



如果您想从App Engine Java应用程序发送短信,您首先需要注册Twilio帐户。一旦您注册了一个账户并且拥有您的账户SID和Auth Token(),则可以在Google应用中遵循本指南引擎文档来设置和配置您的环境以发送消息。



如果遇到任何问题,请通过电子邮件帮助与我们的支持小组联系@ twilio.com。


I was trying to use Twilio's official Java library in my GWT application to send text messages.

Here is the Twilio code I used in my application:

public class TwilioSMS{
/** The Constant ACCOUNT_SID. */
public static final String ACCOUNT_SID = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
public static final String AUTH_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxx";


// Create a rest client
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);


/**
* The main method.
*
* @param args
* the arguments
* @throws TwilioRestException
* the twilio rest exception
*/



public String sendMessage(String _to, String _message) throws TwilioRestException
    {

// Get the main account (The one we used to authenticate the client
Account mainAccount = client.getAccount();

// Send an sms
SmsFactory smsFactory = mainAccount.getSmsFactory();
Map<String, String> smsParams = new HashMap<String, String>();
smsParams.put("To", _to); // Replace with a valid phone number
smsParams.put("From", "(646) 755-7665"); // Replace with a valid phone // number in your account
smsParams.put("Body", _message);
smsFactory.create(smsParams);



// Make a raw request to the api.
TwilioRestResponse resp = client.request("/2010-04-01/Accounts", "GET",
null);
if (!resp.isError()) {
return resp.getResponseText();
}
else
{
return "Failed to send the message.";
}

}

}

When I ran the code in GAE, I got the following exception:

java.lang.NoClassDefFoundError: javax.net.ssl.KeyManagerFactory is a restricted class. Please see the Google App Engine developer's guide for more details.

I did realize that there is a gwt-twilio http://code.google.com/p/gwt-twilio/ but this is a wrapper for twilio client (which does not handle sending text message)

Any examples that send text messages using twilio in GAE+GWT are helpful!

Thanks

Kun

解决方案

I know this is an old one, but I'd like to share a bit more information if I can. As of January 2014, you can use the Twilio helper library for Java on App Engine if you choose. The Twilio Java library's underlying HTTP client implementation has been modified to run on App Engine.

Also, just to be clear, you should not attempt to use the Twilio helper library on the client side with GWT. The Twilio helper library will only work when the code is executed on the server.

If you want to send an SMS from an App Engine Java app, you will first need to sign up for a Twilio account. Once you have signed up for an account and have your Account SID and Auth Token (found on your dashboard), you can follow this guide in the Google App Engine documentation to set up and configure your environment to send a message.

If you run into any problems, please contact our support squad by e-mailing help@twilio.com.

这篇关于如何在Google AppEngine中使用Twilio和GWT(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆