twilio java 与代理身份验证的连接
twilio java connection with proxy authentication
我正在使用 api 示例 here.
中的示例 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 但它每次都 returns 一个 20003 error。 (权限被拒绝)
我怀疑 http-client web 代理身份验证正在覆盖 twilio 帐户 SID 和身份验证令牌,但我不确定,或者是否有解决此问题的方法。
我已经三次检查了我自己的帐户 SID 和授权令牌,还尝试使用 Twilio 为该帐户提供的 "testing" SID 和授权令牌,但我得到了相同的结果。
对 运行通过网络代理使用 Twilio java-sdk 有什么建议吗?
我正在使用 apache http-client 库 4.5.2
已解决...只需要在代理凭据后添加另一组范围为 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));
我会把它留在这里供后代使用,因为对此有几个问题但没有公认的答案。
我正在使用 api 示例 here.
中的示例 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 但它每次都 returns 一个 20003 error。 (权限被拒绝)
我怀疑 http-client web 代理身份验证正在覆盖 twilio 帐户 SID 和身份验证令牌,但我不确定,或者是否有解决此问题的方法。
我已经三次检查了我自己的帐户 SID 和授权令牌,还尝试使用 Twilio 为该帐户提供的 "testing" SID 和授权令牌,但我得到了相同的结果。
对 运行通过网络代理使用 Twilio java-sdk 有什么建议吗?
我正在使用 apache http-client 库 4.5.2
已解决...只需要在代理凭据后添加另一组范围为 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));
我会把它留在这里供后代使用,因为对此有几个问题但没有公认的答案。