19

I am trying to connect to a URL from a desktop app, and I get the error indicated in the Title of my question, but when I tried to connect to the same URL from servlet, all works fine. When I load the URL from browser, all works fine. I am using the same code in the servlet. The code was in a library, when it didn't work, I pulled the code out to a class in the current project, yet it didn't work.

The URL https://graph.facebook.com/me.

The Code fragment.

public static String post(String urlSpec, String data) throws Exception {
    URL url = new URL(urlSpec);
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);
    OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
    writer.write(data);
    writer.flush();

    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line = "";
    StringBuilder builder = new StringBuilder();
    while((line = reader.readLine()) != null) {
        builder.append(line);
    }
    return builder.toString();
}   

I'm a little bit confused here, is there something that is present is a servlet that is not a normal desktop app?

Thanks.

FULL STACK TRACE

Feb 8, 2011 9:54:14 AM com.trinisoftinc.jiraffe.objects.FacebookAlbum create
SEVERE: null
java.io.IOException: Server returned HTTP response code: 400 for URL: https://graph.facebook.com/me
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1313)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
        at com.jiraffe.helpers.Util.post(Util.java:49)
        at com.trinisoftinc.jiraffe.objects.FacebookAlbum.create(FacebookAlbum.java:211)
        at com.trinisoftinc.jiraffe.objects.FacebookAlbum.main(FacebookAlbum.java:261)
0

2 Answers 2

51

EDIT: You need to find the exact error message that facebook is sending in the response You can modify your code to get the message from the error stream like so:

HttpURLConnection httpConn = (HttpURLConnection)connection;
InputStream is;
if (httpConn.getResponseCode() >= 400) {
    is = httpConn.getErrorStream();
} else {
    is = httpConn.getInputStream();
}

Take a look at how you are passing the user context Here's some information that could help you out:

Look at the error message behind the 400 response code:

"Facebook Platform" "invalid_request" "An active access token must be used to query information about the current user*

You'll find the solution here

HTTP/1.1 400 Bad Request
...
WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "An active access token must be used to query information about the current user."
...
Sign up to request clarification or add additional context in comments.

5 Comments

I am passing the same token as I am passing in the Servlet. I am sure the token is correct coz I used it to load the URL on the browser. I appreciate your comment though. I am thinking maybe facebook treats Desktop and Web Apps requests differently, or something like that.
updated the answer; you should peek into the error stream to get the exact message sent to you by the facebook service
@RyanFernandes it's should be the opposite: if (httpConn.getResponseCode() >= 400) { is = httpConn.getErrorStream(); } else { is = httpConn.getInputStream(); }
This is minor but it may be a bit "cleaner" to check like so: if (httpConn.getResponseCode >= HttpURLConnection.HTTP_BAD_REQUEST) Just a thought.
Updated the answer with the correct way round for error/input stream (see Pashok's previous comment).
4

I finally found the problem. Of course it's my code. One part of the code I didn't post is the value of data. data must contain only name and description but I am passing more than name and description.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.