简体   繁体   English

Android Facebook 图 api 基本信息请求

[英]Android Facebook Graph api basic information requesting

just trying to allow my app to access basic information from an authenticated Facebook user, yet my logcat tells me只是试图让我的应用程序从经过身份验证的 Facebook 用户访问基本信息,但我的 logcat 告诉我

06-30 16:37:27.969: WARN/System.err(1559): com.facebook.android.FacebookError: An active access token must be used to query information about the current user. 06-30 16:37:27.969: WARN/System.err(1559): com.facebook.ZC31B32364CE19CA8FCD150A417ECCE5 查询信息必须用于当前用户访问令牌。

right after authentification where the code is ran.在运行代码的身份验证之后。 Can someone point me in the right direction?有人可以指出我正确的方向吗? I've seen a very similar post where someone used almost identical code and It worked.我看过一个非常相似的帖子,其中有人使用了几乎相同的代码并且它有效。

Thanks谢谢

Facebook facebook = new Facebook("187212574660004");

TextView nText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    nText = (TextView) this.findViewById(R.id.nameText);

    facebook.authorize(this, new String[] {"offline_access", "user_interests", "friends_interests"},

            new DialogListener() {
        public void onComplete(Bundle values) {
            setContentView(R.layout.homepage);
        }

        public void onFacebookError(FacebookError error) {}

        public void onError(DialogError e) {}

        public void onCancel() {}
    });

    JSONObject json_data = null;

    try
    {
        JSONObject response = Util.parseJson(facebook.request("me/friends")); // Get a friend information from facebook
        JSONArray jArray = response.getJSONArray("data");

        json_data = jArray.getJSONObject(0);
        String name = json_data.getString("name");
        Log.i("friend is", name);
        nText.setText(name);

    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (FacebookError e)
    {
        e.printStackTrace();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}

Tried the method as described before by Manno23 and got尝试了 Manno23 之前描述的方法并得到了

06-30 19:27:04.338: ERROR/AndroidRuntime(349): Caused by: java.lang.NullPointerException 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at com.staffit.FacebookPage.getFriends(FacebookPage.java:67) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at com.staffit.FacebookPage.access$0(FacebookPage.java:55) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at com.staffit.FacebookPage$1.onComplete(FacebookPage.java:41) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at com.facebook.android.Facebook.authorizeCallback(Facebook.java:383) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at com.staffit.FacebookPage.onActivityResult(FacebookPage.java:93) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at android.app.Activity.dispatchActivityResult(Activity.java:3907) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): at android.app.ActivityThread.deliverResults(ActivityThread.java:2492) 06-30 19:27:04.338: ERROR/AndroidRuntime(349): ... 11 more

There is a lot wrong with this, but I think the first and most important thing for you to grasp is that the call to authorize is asynchronous.这有很多错误,但我认为你要掌握的第一件也是最重要的事情是授权调用是异步的。

This means that onComplete() gets called at a distant time in the future.这意味着 onComplete() 会在未来很远的时间被调用。 Your facebook requests occur "after" the callback definition for onComplete() in the code, but seeing how it is asynchronous those calls are going to happen waaay before the network request for facebook login comes back which means you don't have an auth token when you make that request.您的 facebook 请求发生在代码中 onComplete() 的回调定义“之后”,但是看看它是如何异步的,这些调用将在 facebook 登录的网络请求返回之前发生,这意味着您没有身份验证令牌当您提出该请求时。

*I should add that DialogListener is a connivence class written by facebook to act as way to handle callbacks for async calls. *我应该补充一点,DialogListener 是由 facebook 编写的便利 class ,作为处理异步调用回调的方式。

Both of the above answers are correct.以上两个答案都是正确的。 You can just put the chunk of code that does the request into a separate method and call that method in onComplete(), ie您可以将执行请求的代码块放入单独的方法中并在 onComplete() 中调用该方法,即

Facebook facebook = new Facebook("XXXXXasdasdajksdad");

TextView nText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    nText = (TextView) this.findViewById(R.id.nameText);

    facebook.authorize(this, new String[] {"offline_access", "user_interests", "friends_interests"},

            new DialogListener() {
        public void onComplete(Bundle values) {
            getFriends();
            setContentView(R.layout.homepage);
        }

        public void onFacebookError(FacebookError error) {}

        public void onError(DialogError e) {}

        public void onCancel() {}
    });


}

private void getFriends(){

    JSONObject json_data = null;

    try
    {
        JSONObject response = Util.parseJson(facebook.request("me/friends")); // Get a friend information from facebook
        JSONArray jArray = response.getJSONArray("data");

        json_data = jArray.getJSONObject(0);
        String name = json_data.getString("name");
        Log.i("friend is", name);
        nText.setText(name);

    }
    catch (MalformedURLException e)
    {
        e.printStackTrace();
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (FacebookError e)
    {
        e.printStackTrace();
    }

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}

Just call facebook.request("me/friends") from within onComplete and let the rest of the logic follow from that.只需从 onComplete 中调用facebook.request("me/friends")并让逻辑的 rest 随之而来。

That way it will only call once the Facebook onject has an access token and not if there is an error or some such.这样,它只会在Facebook onject 具有访问令牌时调用,而不是在出现错误或类似情况时调用。 You will probably want to use the other callbacks in the DialogListener for a better flow.您可能希望使用DialogListener中的其他回调来获得更好的流程。 Also think about maintaining that facebook object across the application if you plan on making more requests.如果您打算提出更多请求,还可以考虑在整个应用程序中维护 facebook object。

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

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