简体   繁体   English

Facebook Graph Api OAuthException,但访问令牌有效

[英]Facebook Graph Api OAuthException but access token is valid

I have a problem and I dont know why, I hope you could help me! 我有问题,我不知道为什么,希望您能帮助我!

I am developing an android app which uses the facebook graph api. 我正在开发一个使用Facebook图形API的Android应用。 It uses the readpermission "user_likes". 它使用readpermission“ user_likes”。 When i do this request: "me/likes" everything works fine. 当我执行此请求时: “我/喜欢”,一切正常。 But when i do this: me/likes?fields=events{cover},name 但是当我这样做时: me / likes?fields = events {cover},name

I get an OAuthException . 我收到OAuthException errorMessage: An active access token must be used to query information about the current user errorMessage:必须使用活动访问令牌来查询有关当前用户的信息

In conclusion: 结论:

  1. I get a valid access token with all required permissions. 我获得了具有所有必需权限的有效访问令牌。
  2. I am the administrator of my app so facebook privacy policy should not play any role! 我是我的应用程序的管理员,因此Facebook隐私政策不应该扮演任何角色!
  3. Request 请求

me/likes 我/喜欢

works, but 可行,但是

me/likes?fields=events 我/喜欢?fields =事件

does not work! 不起作用!

Response I get: 我得到的回应:

{Response: responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 2500, errorType: OAuthException, errorMessage: An active access token must be used to query information about the current user.}, isFromCache:false} {响应:responseCode:400,graphObject:null,错误:{HttpStatus:400,errorCode:2500,errorType:OAuthException,errorMessage:必须使用活动访问令牌来查询有关当前用户的信息。},isFromCache:false}

So I debuged my Access-Token: 所以我调试了访问令牌:

Issued: 1420803216 (4 hours ago) 发行:1420803216(4小时前)

Expires: 1425987216 (in about 2 months) 过期:1425987216(大约2个月内)

Valid: True 有效:正确

Origin: Mobile Web Faceweb 来源:Mobile Web Faceweb

Scopes: public_profile, basic_info, email, user_birthday, user_location, user_likes, user_friends 范围:public_profile,basic_info,电子邮件,user_birthday,user_location,user_likes,user_friends


I do not realy understand why my access token has so many scopes? 我不太了解为什么我的访问令牌有这么多范围? I just set "user_likes"! 我只是设置“ user_likes”!

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

This is my Code: 这是我的代码:

 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);
        }
    }

What are I am doing wrong? 我做错了什么? I guess something with my session or permission request is wrong...i hope someone can help me! 我猜我的会话或权限请求有问题...我希望有人可以帮助我!

kind regards christoph 亲切的问候克里斯托夫

That's because, in Facebook SDK for Android, you must pass extra parameters as a Bundle . 这是因为,在Android版Facebook SDK中,您必须将额外的参数作为Bundle传递。

So, for your case, you should invoke the Graph object this way: 因此,对于您的情况,应该以这种方式调用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();

The API call seems to be correct and it works in the API Explorer. 该API调用似乎是正确的,并且可以在API Explorer中运行。 The only logical explanation for the error is that the Access Token is not valid. 该错误的唯一逻辑解释是访问令牌无效。

Put the Token in the Debugger and see if it is valid and if user_likes is available: https://developers.facebook.com/tools/debug/ 将令牌放入调试器,查看其是否有效以及是否有user_likeshttps : //developers.facebook.com/tools/debug/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM