java - 使用 Java API 连接到 Smartsheet 时出错

标签 java ssl smartsheet-api smartsheet-java-sdk-v2

我正在尝试使用他们提供的 java sdk 连接到 smartsheet api。我只是 java 和编程的初学者,对 smartsheet 完全陌生。

这是我的代码(取自 http://smartsheet-platform.github.io/api-docs/?java#generating-access-token )

import com.smartsheet.api.*;
import com.smartsheet.api.models.*;
import com.smartsheet.api.models.enums.SourceInclusion;
import com.smartsheet.api.models.enums.ColumnType;
import com.smartsheet.api.oauth.*;

public class Test {
    public static void main(String[] args) throws SmartsheetException {
        SampleCode();
    }

    public static void SampleCode() throws SmartsheetException {
        // Set the Access Token
        Token token = new Token();
        token.setAccessToken("MY TOKEN");

        // Use the Smartsheet Builder to create a Smartsheet
        Smartsheet smartsheet = new SmartsheetBuilder().setAccessToken(
                token.getAccessToken()).build();

        // Get current user.
        smartsheet.userResources().getCurrentUser();
    }

}

这是我遇到的错误。

Exception in thread "main" com.smartsheet.api.internal.http.HttpClientException: Error occurred.
    at com.smartsheet.api.internal.http.DefaultHttpClient.request(DefaultHttpClient.java:169)
    at com.smartsheet.api.internal.AbstractResources.getResource(AbstractResources.java:192)
    at com.smartsheet.api.internal.UserResourcesImpl.getCurrentUser(UserResourcesImpl.java:179)
    at com.target.test.Test.SampleCode(Test.java:25)
    at com.target.test.Test.main(Test.java:12)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1506)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
    at com.smartsheet.api.internal.http.DefaultHttpClient.request(DefaultHttpClient.java:149)
    ... 4 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1488)
    ... 24 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:146)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
    ... 30 more

最佳答案

错误几乎可以告诉您出了什么问题

unable to find valid certification path to requested target

服务器返回的证书链无效。这可能由于多种原因而发生,最有可能是由于自签名/过期/无效证书。

您可以使用以下 linux 命令检查从服务器返回的证书:

openssl s_client -showcerts -connect yourserverUrl:port

问题很可能出在服务器端,但出于测试目的,您可以通过将证书作为可信证书添加到 keystore 中来解决证书验证错误。这是将证书添加到 keystore 的命令:

keytool -import -trustcacerts -file intermediate.crt -alias intermediateCA -keystore  $JAVA_HOME/jre/lib/security/cacerts

作为此导入的一部分,您将被要求信任该证书,一条消息如下:

Trust this certificate?

只要输入 yes 就可以了。

关于java - 使用 Java API 连接到 Smartsheet 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32391278/

相关文章:

node.js - 在 Shopify 商店的 shop.json 中编辑 force_ssl 选项

c# - Smartsheet c# SDK 父子

java - 防止 Cassandra 转储 hprof 文件

node.js - 如何在 node express socket.io 下设置 SSL?

java - 使用jsp和servlet上传文件

apache - 网站打不开https

python - 如何使用python获取文件夹中的工作表ID数组

python - Smartsheet CHECKBOX 单元格始终返回为空

java - Log4j2 多个记录器具有相同的附加程序但文件名不同

java RMI - 服务器自动调用远程方法