How to use Twilio with GWT in Google AppEngine (Java)

匆匆过客 提交于 2019-12-24 17:07:04

问题


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


回答1:


Twilio Java client library does not on GAE as it obviously uses some Java classes that are not present on GAE.

Since you can not use the Twilio client, your only option is to use GWT-RPC to call your method on server and this method further calls Twilio REST API.




回答2:


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.



来源:https://stackoverflow.com/questions/8346657/how-to-use-twilio-with-gwt-in-google-appengine-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!