无法在Android App中使用facebook api获取访问令牌

时间:2015-11-13 12:47:34

标签: android facebook facebook-graph-api

我需要关于facebook的accessToken的帮助,这里的问题是当我尝试调用accessToken方法时它返回null。实际上我想将我的应用中的评论发布到特定页面,并从发布这些帖子的页面ID中获取评论。

我遇到的问题是,当我尝试发表评论时,我收到类似的错误必须使用活动访问令牌来查询有关当前用户的信息。

当我试图收到评论时我收到此错误要求此资源需要访问令牌

我被困在这里需要帮助。

感谢您的帮助!!

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    linearLayout2 = (LinearLayout) inflater.inflate(R.layout.second_fragment, container, false);

    editText = (EditText) linearLayout2.findViewById(R.id.commentBox);
    button = (Button) linearLayout2.findViewById(R.id.submitButton);

    openfeed = (LinearLayout) linearLayout2.findViewById(R.id.fakebutton);

    initInstances();
    initCallbackManager();
    refreshButtonState();
    getAllComments();


    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle params = new Bundle();
            params.putString("message", editText.getText().toString());
            GraphRequest graphRequest = new GraphRequest(
                    accessToken,
                    "/me/feed",
                    params,
                    HttpMethod.POST,
                    new GraphRequest.Callback() {
                        @Override
                        public void onCompleted(GraphResponse response) {
                            System.out.println("Success: " + response);
                        }
                    }
            );
            graphRequest.executeAsync();
        }
    });
    return linearLayout2;
}

private void initInstances() {
    System.out.println("I am init");
    openfeed.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LoginManager.getInstance().logInWithPublishPermissions(getActivity(), Arrays.asList("publish_actions,publish_pages,manage_pages,user_about_me"));
        }
    });
}


private void initCallbackManager() {
    System.out.println("i am initCallbackManager");
    callbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            refreshButtonState();
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException error) {

        }
    });

}

private void refreshButtonState() {
    System.out.println("refresh button state");
    if (!isLoggedIn()) {
        openfeed.setVisibility(View.VISIBLE);
    } else {
        openfeed.setVisibility(View.GONE);
    }
}

private boolean isLoggedIn() {

    accessToken = AccessToken.getCurrentAccessToken();

    return  accessToken != null;
}

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

    callbackManager.onActivityResult(requestCode, resultCode, data);
}

public void getAllComments() {
    new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "/{page-id}/feed",
            null,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    System.out.println("comments: " + response);
                }
            }
    ).executeAsync();
}

}

0 个答案:

没有答案