【问题标题】:Posting to Facebook feed from Android app从 Android 应用程序发布到 Facebook 提要
【发布时间】:2011-04-13 07:08:08
【问题描述】:

我正在尝试让已链接其 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”操作一起使用,但这会导致出现不需要的对话框。

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

现在,我正在尝试使用 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 访问令牌。有人对如何进行有建议吗?

【问题讨论】:

  • facebook中的访问令牌是什么意思

标签: android facebook rest oauth


【解决方案1】:

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

final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);

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

解决方案是在添加令牌之前对其进行解码。
例如:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多