【问题标题】:Invalid OAUTH access token, when using an application access token, Android无效的 OAUTH 访问令牌,当使用应用程序访问令牌时,Android
【发布时间】:2012-06-21 10:39:58
【问题描述】:

正如标题所说,当我尝试请求获取已安装字段的朋友列表时:

"me/friends?Fields=installed&access_token=..."

我的 logcat 中出现以下错误:

"Invalid OAuth access token"

在查看 facebook api 时,我看到安装需要获取应用程序访问令牌。所以我使用 appId 和 app Secret 生成了应用程序访问令牌:

https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=APP_ID&client_secret=APP_SECRET

下面是代码:

try {

 JSONObject obj = Util.parseJson(mFacebook.request("me/friends?fields=installed&access_token=..."));

  Log.d("json Response", obj.toString());
  JSONArray array = obj.optJSONArray("data");
  if (array != null) {
                      for (int i = 0; i < array.length(); i++) {
                        //  String name = array.getJSONObject(i).getString("name");

                          String id = array.getJSONObject(i).getString("id");

                          Log.d("Friends: ",id);
                      }
                  } 
  }catch (Exception e){
    Log.d("Friends:", e.getMessage());
  }

任何人知道为什么这样做我一直在寻找年龄。

干杯

【问题讨论】:

  • 你能发一些代码吗?
  • 是的,我只需要更换电脑。将编辑我的原始帖子

标签: android facebook oauth access-token


【解决方案1】:

认证后得到的是App Access Token。

引用:Authenticating as an App allows you to obtain an access token which allows you to make request to the Facebook API on behalf of an App rather than a User. This is useful, for example to modify the parameters of your App, create and manage test users, or read your application's insights for example. App access tokens can also be used to publish content to Facebook on behalf of a user who has granted a publishing permission to your application

来自facebook docs。

您的案例需要的是用户访问令牌。请参阅this。 用户需要对您的应用进行身份验证并授予访问权限,您才能访问他的好友列表。

编辑

用于检查单个用户的 url 是 https://graph.facebook.com/{uid}?fields=installed&access_token=

您正在尝试获取用户好友列表,然后检查该应用是否已安装。我认为没有用户访问令牌是不可能获取用户朋友列表的。

编辑 2

从您的评论中假设您有 frind 的列表。然后您无需为每个用户调用,而是可以使用 FQL。

https://graph.facebook.com/fql?q=SELECT uid,is_app_user from user where uid IN (uid1,uid2)

如果你有用户访问令牌,那么你可以直接这样做。

https://graph.facebook.com/fql?q=SELECT uid,is_app_user from user where uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

希望这能解决您的问题。

【讨论】:

  • 我是这样工作的,但是当我浏览我的朋友列表时,它一次大约有 400 人,它真的很慢,并将其与一些将拥有 1000 人的朋友列表的人进行比较会太慢。所以想知道你是否可以让它一次性完成整个朋友列表。
  • 编辑了我的答案。看看有没有帮助。
【解决方案2】:

不要请求 /me/friends - 因为使用应用访问令牌,API 将不知道“我”应该是谁 -而是请求 /userid/friends。

抱歉,我之前错了 - 方法是使用用户访问令牌,然后将请求设置为针对 /me/friends...

【讨论】:

  • 当我尝试使用 facebook.getAccessToken() 时,我收到一条错误消息,提示我使用了格式错误的访问令牌
  • @PhilDarnell,和我一样的问题,现在,你解决了吗?
  • 这似乎是一些奇怪的错误;使用 (otherparams)&access_token=(facebook.getAccessToken()) 时它不起作用,但是当我将 access_token 放在 url 的前面时 ?access_token=(facebook.getAccessToken())&(otherparams) 它确实有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多