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)
devsri
  • 6,173
  • 5
  • 32
  • 40
Akintayo Olusegun
  • 917
  • 1
  • 10
  • 20

2 Answers2

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."
...
Community
  • 1
  • 1
Ryan Fernandes
  • 8,238
  • 7
  • 36
  • 53
  • 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. – Akintayo Olusegun Feb 08 '11 at 09:36
  • updated the answer; you should peek into the error stream to get the exact message sent to you by the facebook service – Ryan Fernandes Feb 08 '11 at 10:01
  • 1
    @RyanFernandes it's should be the opposite: if (httpConn.getResponseCode() >= 400) { is = httpConn.getErrorStream(); } else { is = httpConn.getInputStream(); } – Pashok Jun 30 '12 at 17:31
  • This is minor but it may be a bit "cleaner" to check like so: if (httpConn.getResponseCode >= HttpURLConnection.HTTP_BAD_REQUEST) Just a thought. – Will Madison Oct 22 '12 at 14:52
  • 1
    Updated the answer with the correct way round for error/input stream (see Pashok's previous comment). – Steve Chambers Jan 29 '13 at 10:00
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.

Akintayo Olusegun
  • 917
  • 1
  • 10
  • 20