【问题标题】:Facebook Graph Api OAuthException but access token is validFacebook Graph Api OAuthException 但访问令牌有效
【发布时间】:2015-03-07 12:40:07
【问题描述】:

我有一个问题,我不知道为什么,希望你能帮助我!

我正在开发一个使用 facebook graph api 的 android 应用程序。 它使用读取权限“user_likes”。当我提出这个要求时: “我/喜欢” 一切正常。但是当我这样做时: me/likes?fields=events{cover},name

我得到一个 OAuthExceptionerrorMessage:必须使用活动访问令牌来查询有关当前用户的信息

总结:

  1. 我获得了具有所有必需权限的有效访问令牌。
  2. 我是我的应用程序的管理员,因此 Facebook 隐私政策不应发挥任何作用!
  3. 请求

我/喜欢

有效,但是

我/喜欢?fields=events

不起作用!

我得到的回应:

{Response: responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 2500, errorType: OAuthException, errorMessage: 必须使用活动访问令牌来查询当前用户的信息。}, isFromCache:错误}

所以我调试了我的访问令牌:

发布:1420803216(4小时前)

过期:1425987216(大约 2 个月后)

有效:真

来源:Mobile Web Faceweb

范围:public_profile、basic_info、email、user_birthday、user_location、user_likes、user_friends


我真的不明白为什么我的访问令牌有这么多范围?我只是设置了“user_likes”!

loginBtn.setReadPermissions(Arrays.asList("user_likes"));

这是我的代码:

 private LoginButton loginBtn;
    private TextView username;
    private UiLifecycleHelper uiHelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        uiHelper = new UiLifecycleHelper(this, statusCallback);
        uiHelper.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        username = (TextView) findViewById(R.id.username);
        loginBtn = (LoginButton) findViewById(R.id.fb_login_button);
        loginBtn.setReadPermissions(Arrays.asList("user_likes"));
        loginBtn.setUserInfoChangedCallback(new UserInfoChangedCallback() {
            public void onUserInfoFetched(GraphUser user) {
                if (user != null) {
                    username.setText("You are currently logged in as "
                            + user.getName());
                } else {
                    username.setText("You are not logged in.");
                }
            }
        });
    }

    private Session.StatusCallback statusCallback = new Session.StatusCallback() {
        public void call(Session session, SessionState state,
                Exception exception) {
            if (state.isOpened()) {
                Log.d("MainActivity", "Facebook session opened.");
            } else if (state.isClosed()) {
                Log.d("MainActivity", "Facebook session closed.");
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        Session session = Session.getActiveSession();
        if (session != null && (session.isOpened() || session.isClosed())) {
            onSessionStateChange(session, session.getState(), null);
        }
        uiHelper.onResume();
    }

    private void onSessionStateChange(final Session session,
            SessionState state, Exception exception) {

        if (state.isOpened()) {
            // userInfoTextView.setVisibility(View.VISIBLE);
            String token = session.getAccessToken();
            Log.d("SESSION_TOKEN", token);
            new Request(session, "me/likes?fields=events", null, HttpMethod.GET,
                    new Request.Callback() {
                        public void onCompleted(Response response) {
                            Log.i("INFO", response.toString());
                            // new
                            // GetResponses(userInfoTextView).execute(response);
                        }
                    }).executeAsync();

        } else if (state.isClosed()) {
            // userInfoTextView.setVisibility(View.INVISIBLE);
        }
    }

我做错了什么?我猜我的会话或权限请求有问题...希望有人能帮助我!

亲切的问候 克里斯托夫

【问题讨论】:

    标签: facebook facebook-graph-api facebook-android-sdk facebook-graph-api-v2.0 facebook-permissions


    【解决方案1】:

    这是因为,在 Android 版 Facebook SDK 中,您必须将额外参数作为 Bundle 传递。

    因此,对于您的情况,您应该以这种方式调用 Graph 对象:

    Bundle bundle = new Bundle();
    bundle.putString("fields", "events");
    
    new GraphRequest(AccessToken.getCurrentAccessToken(), "me/likes", bundle, HttpMethod.GET, new GraphRequest.Callback() {
        public void onCompleted(GraphResponse response) {
            // Your stuff here
        }
    }).executeAsync();
    

    【讨论】:

      【解决方案2】:

      API 调用似乎是正确的,并且可以在 API Explorer 中运行。该错误的唯一合乎逻辑的解释是访问令牌无效。

      将 Token 放入 Debugger 看是否有效,user_likes 是否可用:https://developers.facebook.com/tools/debug/

      【讨论】:

      • 已经完成了... 2个月左右过期,有效,所有权限都可用!范围 public_profile、user_birthday、user_location、user_likes、user_events
      • 那么你没有在调用中使用访问令牌,这将是唯一的解释,因为它在 api explorer 中工作正常。
      • 我在图形资源管理器中尝试了来自 android-app 的访问令牌,它成功了!但是当我在 android 应用程序中执行相同的请求时,我总是会收到错误消息。 screenshot
      • @luschn - 我遇到了一个类似的问题,我正在使用 JavaScript SDK 创建一个 facebook 页面 Web 应用程序,尽管我查询了我的页面提交的帖子列表并且有一个有效的页面访问令牌相关权限publish_pages,将帖子发布为页面将失败...直到我实际上将access_token 传递到POST 调用FB.api("/page_id/feed", "post", {message: message, access_token: page_access_token}, callback_handler(response){})
      猜你喜欢
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 2015-02-13
      • 2012-03-04
      • 1970-01-01
      相关资源
      最近更新 更多