当我尝试获取启动经过身份验证的客户端所需的授权代码时,我收到的响应有问题。响应似乎(至少对我而言)是某种二进制数据,(其中包含一个或两个可识别的字符串片段),但我不知道如何处理它。API 的“hello world”程序中的代码似乎将这些数据解析为字符串,但是当我尝试它时,它失败了,因为它不是预期的格式。
这是改编自此处找到的“Hello World”程序(来自 box api github wiki) ,以显示问题所在。
import java.awt.Desktop;
import java.io.*;
import java.net.*;
public class HelloBox {
public static final int socket_port = 4000;
public static final String
redirect_uri = "https://localhost:" + socket_port,
client_id = /* [...] */, client_secret = /* [...] */;
public static void main(String[] args) throws Exception{
Desktop.getDesktop().browse(URI.create(
"https://www.box.com/api/oauth2/authorize?" +
"response_type=code" +
"&client_id=" + client_id +
"&redirect_uri=" + redirect_uri));
ServerSocket serverSocket = new ServerSocket(socket_port);
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
for(int idx = 0; idx < 4; idx++){
String line = in.readLine();
System.out.println(line);
out.println(line);
}
//[... closing all the streams and sockets ...]
}
}
第四行之后的所有行都是 just null
,所以这就是我在四行之后停止它的原因。
您可以在我的output.txt
文件中看到结果。我的电脑拒绝粘贴字符串,所以我不能将它本身包含在问题中。我尝试使用十六进制编辑器检查输出,但看不到任何明显的模式。(突发的东西被偶尔分开NULL
?)
无数的 Google 搜索结果似乎与此问题无关,Javadoc 和 API 教程在这里也没有帮助。
我该如何处理这个问题?如果我的代码有误,请告诉我如何更正它。
编辑:所以我尝试将socket
andserverSocket
替换为
SSLServerSocket serverSocket = (SSLServerSocket) SSLServerSocketFactory.getDefault().createServerSocket(socket_port);
SSLSocket socket = (SSLSocket) serverSocket.accept();
但现在我到达
javax.net.ssl.SSLHandshakeException: no cipher suites in common
了它第一次尝试从流中读取的那一行。