You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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);
    }
}

关键说明

  1. 全局生效:通过HttpClients.custom()配置的代理认证会对所有SDK发起的请求生效,无需单独为每个请求配置
  2. 令牌替换:请将YOUR_SMARTSHEET_ACCESS_TOKEN替换为你的有效Smartsheet访问令牌
  3. 版本兼容:Smartsheet Java SDK 2.25版本完全支持setHttpClient()方法,可直接替换底层HTTP客户端实现

内容的提问来源于stack exchange,提问作者Sean Li

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 09:02:18