我正在使用api示例here中的示例Twilio java-sdk设置java Twilio连接。
唯一的区别是我需要通过带有身份验证的Web代理运行我的Twilio连接。
关于通过网络代理连接到Twilio还有其他问题,但没有接受的答案。例如this,基于该答案我尝试实施这样的解决方案:
import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.MessageFactory;
import com.twilio.sdk.resource.instance.Message;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.message.BasicNameValuePair;
import java.util.ArrayList;
import java.util.List;
public class Example {
// Find your Account Sid and Token at twilio.com/console
public static final String ACCOUNT_SID = "asdfasdfasdfdasf";
public static final String AUTH_TOKEN = "asdfasdfasdfasdf";
public static final String PROXY_ADDRESS = "1.2.3.4";
public static final int PROXY_PORT = 80;
public static final String PROXY_USER = "user";
public static final String PROXY_PASSWORD = "password";
public static void main(String[] args) {
//Set up Proxy host
HttpHost proxy = new HttpHost(PROXY_ADDRESS, PROXY_PORT);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
//Set up Proxy user credentials
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(PROXY_ADDRESS, PROXY_PORT),
new UsernamePasswordCredentials(PROXY_USER, PROXY_PASSWORD));
//Set up HttpClient with proxy and credentials
CloseableHttpClient httpClient = HttpClients.custom()
.setRoutePlanner(routePlanner)
.setDefaultCredentialsProvider(credsProvider)
.build();
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
client.setHttpClient(httpClient);
// Build a filter for the MessageList
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Body", "Hello from Java"));
params.add(new BasicNameValuePair("To", "+12345678901"));
params.add(new BasicNameValuePair("From", "+12345678901"));
MessageFactory messageFactory = client.getAccount().getMessageFactory();
try {
Message message;
message = messageFactory.create(params);
System.out.println(message.getSid());
} catch (TwilioRestException e) {
System.out.println(e.getErrorCode());
System.out.println(e.getErrorMessage());
e.printStackTrace();
}
}
}
似乎通过代理使用它并到达Twilio API,但每次都返回20003 error。 (许可被拒绝)
我怀疑http客户端Web代理身份验证会覆盖twilio帐户的SID和身份验证令牌,但我不确定,或者是否有解决方法。
我已经检查了我自己的帐户SID和身份验证令牌,并尝试使用&#34;测试&#34; Twilio为帐户提供的SID和身份验证令牌,但我得到的结果相同。
有关通过Web代理运行Twilio java-sdk的任何建议吗?
我正在使用apache http-client library 4.5.2
答案 0 :(得分:1)
已解决...只需在代理凭据后添加另一组范围为api.twilio.com
的凭据:
//Set up Proxy user credentials
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(PROXY_ADDRESS, PROXY_PORT),
new UsernamePasswordCredentials(PROXY_USER, PROXY_PASSWORD));
//Set up Twilio user credentials
credsProvider.setCredentials(
new AuthScope("api.twilio.com", 443),
new UsernamePasswordCredentials(ACCOUNT_SID, AUTH_TOKEN));
我将这里留给后人,因为有几个问题,但没有接受答案。