问题
I am having an issue with the response I am getting when I try to get the authorization code I need to start the authenticated client. The response appears (at least to me) to be some sort of binary data, (with one or two recognizable string fragments in it), but I have no idea how to deal with it. The code from the API's 'hello world' program seems to be parsing this data as a string, but when I try it, it fails because it isn't in the expected format.
This is adapted from the 'Hello World' program (from the box api github wiki) found here in order to show what the problem appears to be.
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 ...]
}
}
All the lines after the fourth are just null
, so that is why I stopped it after four lines.
You can see the result in my output.txt
file here. My computer refuses to paste the string, so I couldn't include it itself in the question. I have tried examining the output with a hex editor, but I couldn't see any obvious pattern to it. (Bursts of stuff separated by occasional NULL
?)
Myriad Google searches turned up nothing that appears to be of relevance for this issue, and neither the Javadoc nor the API tutorials were helpful here, either.
How can I deal with this issue? If my code is wrong, please tell me how I can correct it.
EDIT:
So I tried replacing the socket
and serverSocket
with
SSLServerSocket serverSocket = (SSLServerSocket) SSLServerSocketFactory.getDefault().createServerSocket(socket_port);
SSLSocket socket = (SSLSocket) serverSocket.accept();
but now I am getting
javax.net.ssl.SSLHandshakeException: no cipher suites in common
at the line where it first tries to read from the stream.
来源:https://stackoverflow.com/questions/17221756/how-do-i-get-text-data-from-oauth-callback