【问题标题】:Using Dropbox Java api: DropboxSSLException: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated使用 Dropbox Java api: DropboxSSLException: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
【发布时间】:2013-01-04 11:07:40
【问题描述】:

我正在尝试使用 Dropbox Java API 让我的 Java 应用程序上传文件。不知何故,我最终遇到了我无法解决的相同 SSL 错误。有什么建议吗?

我使用了这段代码 (http://aaka.sh/patel/2011/12/20/authenticating-dropbox-java-api/)

    public class DropboxTest {

    // App key & secret that Dropbox's developer website gives your app
    private static final String APP_KEY = "myAppKey";
    private static final String APP_SECRET = "myAppSecret";
    // Define AccessType for DropboxAPI object
    final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
    private static DropboxAPI<WebAuthSession> mDBApi;

    public static void main(String[] args) throws Exception{

        // Initialize the goods/session
        AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
        WebAuthSession session = new WebAuthSession(appKeys, ACCESS_TYPE);

        // Initialize DropboxAPI object
        mDBApi = new DropboxAPI<WebAuthSession>(session);

        // Get ready for user input
        Scanner input = new Scanner(System.in);

        // Open file that stores tokens, MUST exist as a blank file
        File tokensFile = new File("TOKENS");

        System.out.println("Enter 'a' to authenticate, or 't' to test reauthentication: ");
        String command = input.next();

        if(command.equals("a")){

            try {

                // Present user with URL to allow app access to Dropbox account on
                System.out.println("Please go to this URL and hit \"Allow\": " + mDBApi.getSession().getAuthInfo().url);
                AccessTokenPair tokenPair = mDBApi.getSession().getAccessTokenPair();

                // Wait for user to Allow app in browser
                System.out.println("Finished allowing?  Enter 'next' if so: ");
                if(input.next().equals("next")){
                    RequestTokenPair tokens = new RequestTokenPair(tokenPair.key, tokenPair.secret);
                    mDBApi.getSession().retrieveWebAccessToken(tokens);
                    PrintWriter tokenWriter = new PrintWriter(tokensFile);
                    tokenWriter.println(session.getAccessTokenPair().key);
                    tokenWriter.println(session.getAccessTokenPair().secret);
                    tokenWriter.close();
                    System.out.println("Authentication Successful!");

                }

            } catch (DropboxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        else if(command.equals("t")){

            Scanner tokenScanner = new Scanner(tokensFile);       // Initiate Scanner to read tokens from TOKEN file
            String ACCESS_TOKEN_KEY = tokenScanner.nextLine();    // Read key
            String ACCESS_TOKEN_SECRET = tokenScanner.nextLine(); // Read secret
            tokenScanner.close(); //Close Scanner 

            //Re-auth
            AccessTokenPair reAuthTokens = new AccessTokenPair(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET);
            mDBApi.getSession().setAccessTokenPair(reAuthTokens);
            System.out.println("Re-authentication Sucessful!");

            //Run test command
            System.out.println("Hello there, " + mDBApi.accountInfo().displayName);

        }
    }
}

以 SSL 异常结束:

com.dropbox.client2.exception.DropboxSSLException: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
    at com.dropbox.client2.RESTUtility.execute(RESTUtility.java:416)
    at com.dropbox.client2.RESTUtility.execute(RESTUtility.java:337)
    at com.dropbox.client2.RESTUtility.streamRequest(RESTUtility.java:192)
    at com.dropbox.client2.session.WebAuthSession.setUpToken(WebAuthSession.java:218)
    at com.dropbox.client2.session.WebAuthSession.getAuthInfo(WebAuthSession.java:158)
    at com.dropbox.client2.session.WebAuthSession.getAuthInfo(WebAuthSession.java:128)
    at DropboxTest.main(DropboxTest.java:45)
Caused by: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
    at sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:397)
    at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
    at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:399)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:108)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
    at com.dropbox.client2.RESTUtility.execute(RESTUtility.java:385)
    ... 6 more

【问题讨论】:

  • 您使用的是什么版本的 Java?
  • 1.7 但 1.6 给出了同样的错误
  • 我刚刚去了dropbox.com/developers/reference/api,并提供了一个不受信任的 SSL 证书(我使用的是 Chrome 19),所以这很可能是同一个问题。
  • 是的,这与 Dropbox 客户端无关(见根本原因),只是一个不受信任的证书
  • 如何让我的客户端应用程序信任该证书?

标签: java ssl dropbox dropbox-api


【解决方案1】:

想通了。我在 OSX 上使用 OpenJDK。 我使用-Djavax.net.debug=ssl 运行 JVM 进行调试,结果是根异常

java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

经过一番研究,我在这里找到了解决方案: http://architecturalatrocities.com/post/19073788679/fixing-the-trustanchors-problem-when-running-openjdk-7

现在工作正常!

【讨论】:

    猜你喜欢
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 2014-03-21
    • 2013-08-18
    • 1970-01-01
    • 2014-07-08
    • 1970-01-01
    相关资源
    最近更新 更多