如何在Smartsheet Java SDK 2.25中实现带CredentialsProvider的代理认证?
在Smartsheet Java SDK 2.25中实现带认证的代理配置
Smartsheet Java SDK底层基于Apache HttpClient,你可以通过替换SDK默认的HttpClient实例来注入代理认证信息,具体实现如下:
实现步骤
- 创建包含代理认证信息的
CredentialsProvider,逻辑与你现有Apache代码一致 - 构建整合代理配置与认证信息的自定义
CloseableHttpClient - 将自定义HttpClient传入Smartsheet客户端构建器完成配置
完整示例代码
import com.smartsheet.api.Smartsheet; import com.smartsheet.api.SmartsheetBuilder; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; public class SmartsheetProxyAuthDemo { public static void main(String[] args) throws Exception { // 1. 配置代理认证凭证 CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("proxy", 8080), new UsernamePasswordCredentials("username", "password") ); // 2. 构建带代理认证的自定义HttpClient CloseableHttpClient customHttpClient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider) .build(); // 3. 初始化Smartsheet客户端并绑定自定义HttpClient Smartsheet smartsheet = new SmartsheetBuilder() .setAccessToken("YOUR_SMARTSHEET_ACCESS_TOKEN") .setHttpClient(customHttpClient) .build(); // 4. 测试代理下的API调用(示例:获取用户工作表列表) System.out.println("通过认证代理调用Smartsheet API..."); smartsheet.sheetResources().listSheets(null, null, null); } }
关键说明
- 全局生效:通过
HttpClients.custom()配置的代理认证会对所有SDK发起的请求生效,无需单独为每个请求配置 - 令牌替换:请将
YOUR_SMARTSHEET_ACCESS_TOKEN替换为你的有效Smartsheet访问令牌 - 版本兼容:Smartsheet Java SDK 2.25版本完全支持
setHttpClient()方法,可直接替换底层HTTP客户端实现
内容的提问来源于stack exchange,提问作者Sean Li
相关产品推荐
相关产品推荐

