我正在使用此处api 示例中的示例 Twilio java-sdk 设置 java Twilio 连接。
唯一的区别是我需要通过具有身份验证的网络代理运行我的 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 错误。(没有权限)
我怀疑 http-client Web 代理身份验证正在覆盖 twilio 帐户 SID 和身份验证令牌,但我不确定,或者是否有解决方法。
我已经三次检查了我自己的帐户 SID 和身份验证令牌,还尝试使用 Twilio 为该帐户提供的“测试”SID 和身份验证令牌,但我得到了相同的结果。
通过网络代理运行 Twilio java-sdk 有什么建议吗?
我正在使用 apache http-client 库 4.5.2