2011-12-01 43 views
1

我試圖在我的GWT應用程序中使用Twilio的官方Java庫來發送短信。如何在Google AppEngine中使用GWT的Twilio(Java)

這裏是Twilio代碼,我在我的應用程序中使用:

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."; 
} 

} 

} 

當我運行在GAE的代碼中,我得到了以下異常:

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

我也認識到,有一個gwt-twilio http://code.google.com/p/gwt-twilio/但是這是twilio客戶端的封裝器(它不處理髮送文本消息)

任何使用GAE + GWT中的twilio發送文本消息的示例e有幫助!

感謝

回答

0

Twilio Java客戶端庫並不在GAE上,因爲它明顯採用的是不存在的GAE一些Java類。

由於您無法使用Twilio客戶端,因此您唯一的選擇是使用GWT-RPC在服務器上調用您的方法,並且此方法還會調用Twilio REST API。

+0

感謝彼得......上面的Twilio類在服務器端,它在一個由GWT-RPC請求調用的方法中。但是,GAE仍在抱怨_java.lang.NoClassDefFoundError:javax.net.ssl.KeyManagerFactory是一個受限制的類。請參閱Google App Engine開發人員指南瞭解更多詳情._ – enfany

+0

Kun,我認爲他意味着您可能必須自己進行HTTP調用,而不使用Twilio客戶端,因爲Twilio客戶端的類不是'在應用引擎上列入白名單。下面是一篇關於如何使用Apache的HttpClient4來解決URL Fetch API限制的文章: http://esxx.blogspot.com/2009/06/using-apaches-httpclient-on-google-app.html –

0

我知道這是一箇舊的,但我想分享一些信息,如果我可以。截至2014年1月,如果您願意,可以在App Engine上使用Twilio助手庫。 Twilio Java庫的基礎HTTP客戶端實現已被修改爲在App Engine上運行。

此外,爲了清楚起見,您不應該嘗試在GWT客戶端使用Twilio幫助程序庫。 Twilio輔助程序庫僅在代碼在服務器上執行時纔可用。

如果您想從App Engine Java應用程序發送短信,您首先需要sign up for a Twilio account。一旦您註冊了一個賬戶並擁有您的賬戶SID和授權令牌(found on your dashboard),您可以follow this guide in the Google App Engine documentation來設置和配置您的環境以發送消息。

如果遇到任何問題,請通過電子郵件[email protected]與我們的支持小組聯繫。

相關問題