从Android应用发布到Facebook Feed

时间:2010-09-16 15:33:23

标签: android facebook rest oauth

我正在尝试启用已关联其Facebook帐户的用户,只需按一下按钮detailed in their docs即可发布到他们的墙上。具体来说,我不想使用Facebook for Android library中提供的.dialog方法,因为我希望发布过程尽可能无缝。

特定呼叫在HTTP客户端模拟器中如下所示:
HTTP POST
https://graph.facebook.com/me/feed
内容类型:application / x-www-form-urlencoded
你认为access_token = ...& link = http://mylink.com/id/1326&name=What?& description =链接描述

此调用在OS X上的HTTP客户端中成功运行。

我最初尝试使用Facebook for Android库的.dialog函数和“stream.publish”操作,但这会导致出现不需要的对话框。

接下来,我尝试使用Facebook for Android的.request函数和“POST”参数,但是这个函数假定正文是一个字节数组,并在库中的多个位置失败。

现在,我正在尝试使用Android附带的Apache HTTP堆栈。 具体来说,我的代码如下所示:

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.

问题在于Facebook的反应始终如下:

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

我知道OAuth访问令牌无效。当我将相同的OAuth访问令牌粘贴到HTTP客户端时,它就像一个冠军。似乎创建HttpPost对象的参数以某种方式使复杂的OAuth访问令牌变得混乱。有人有关于如何进行的建议吗?

1 个答案:

答案 0 :(得分:0)

显然,Facebook的access_token已经是URLEncoded。 所以,这一行:

final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);

对令牌进行双重编码,使其无效。

解决方法是在添加令牌之前解码令牌 E.g:

pairs.add(new BasicNameValuePair("access_token", URLDecoder.decode(App.facebook.getAccessToken(), "UTF-8"));