views:

112

answers:

1

Hi all, I am trying to enable users who have linked their Facebook account to be able to post to their wall with one button press as detailed in their docs. Specifically, I do not want to use the .dialog methods provided in the Facebook for Android library, as I want the Publish process to be as seamless as possible.

The specific call looks like this in an HTTP client simulator:
HTTP POST
https://graph.facebook.com/me/feed
Content-type: application/x-www-form-urlencoded
access_token=...&link=http://mylink.com/id/1326&name=What do you think?&description=Description of the link

This call runs successfully in HTTP Client on OS X.

I originally tried using the Facebook for Android library's .dialog function with "stream.publish" action, but this causes the unwanted dialog to appear.

Next, I tried using Facebook for Android's .request function with "POST" parameter, but this function assumes that the body is a byte array and fails in multiple places in the library.

Now, I am trying to use the Apache HTTP stack included with Android. Specifically, my code looks like this:

String url = "https://graph.facebook.com/me/feed";

ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>(4);
pairs.add(new BasicNameValuePair("access_token", App.facebook.getAccessToken()));
pairs.add(new BasicNameValuePair("link", "http://mylink.com/id/"+id));
pairs.add(new BasicNameValuePair("name", question));
pairs.add(new BasicNameValuePair("description", description));

InputStream inputStream = null;
HttpResponse response = null;
HttpClient client = null;
try {
    final HttpPost post = new HttpPost(url);
    final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);
    post.setEntity(entity);
    client = new DefaultHttpClient();
    response = client.execute(post);
} catch (IOException e) {
    // code to handle exceptions
}
// close streams, etc.

The problem is that the response from Facebook is consistently the following:

{"error":{"type":"OAuthException","message":"Invalid OAuth access token."}}

I know the OAuth access token is not invalid. When I paste the same OAuth access token into HTTP Client, it works like a champ. It seems that creating the HttpPost object's parameters is garbling the complex OAuth access token in some way. Does anyone have suggestions on how to proceed?

A: 

Apparently, Facebook's access_token is already URLEncoded. So, this line:

final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);

double-encodes the token, which renders it invalid.

The solution is to decode the token before adding it.
E.g.:

pairs.add(new BasicNameValuePair("access_token", URLDecoder.decode(App.facebook.getAccessToken(), "UTF-8"));
js01
Looks like this might be another solution: http://github.com/facebook/facebook-android-sdk/issues/issue/29
js01