简体   繁体   English

Facebook登录无法通过应用程序运行

[英]Facebook Login not working through app

I've got a problem logging in with facebook on my app. 我在应用程序上使用Facebook登录时遇到问题。 Logging in only works when the Android Facebook app is not installed. 仅在未安装Android Facebook应用程序时登录。 In that case you'll get a popup from facebook. 在这种情况下,您会从Facebook弹出窗口。 When the Facebook app is installed, the login will redirect to the app. 安装Facebook应用程序后,登录名将重定向到该应用程序。 In this case I don't get my session token back, instead I get the message: CLOSED_LOGIN_FAILED 在这种情况下,我不会找回会话令牌,而是会收到以下消息:CLOSED_LOGIN_FAILED

I've been looking all over the place, but I really can't find the problem. 我一直在到处找,但我确实找不到问题。

This is the code in my Fragment, The login button is created through XML: 这是我的Fragment中的代码,登录按钮是通过XML创建的:

private void initButtons()
    {
            btnFacebook.setApplicationId(v.getContext().getResources().getString(R.string.app_id));
            btnFacebook.setReadPermissions(Arrays.asList("email", "public_profile", "user_birthday"));
            btnFacebook.setFragment(this);
            btnFacebook.setSessionStatusCallback(new StatusCallback()
            {

              @Override
              public void call(Session session, SessionState state, Exception exception)
              {
                Log.d("Token:",session.getAccessToken());
              }

            });
    }

public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this.getActivity(), requestCode, resultCode, data);

}

Any ideas what I might be missing? 有什么想法可能会丢失吗? Again, the login only works without the facebook app installed 同样,登录仅在未安装facebook应用程序的情况下有效

EDIT: I get a Session Closed Login Failed message when the account which is used, already registered the app. 编辑:当使用的帐户已经注册了应用程序时,我收到会话关闭登录失败消息。 When user first logs in using this app (added to their account) the results are as they should be 用户首次使用此应用(添加到他们的帐户)登录时,结果应为

Use the below given class as a Facebook helper that might deal with you error. 使用下面给定的类作为Facebook帮助程序,它可能会处理您的错误。

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;

import com.facebook.FacebookException;
import com.facebook.FacebookOperationCanceledException;
import com.facebook.FacebookServiceException;
import com.facebook.HttpMethod;
import com.facebook.LoggingBehavior;
import com.facebook.Request;
import com.facebook.RequestAsyncTask;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Settings;
import com.facebook.widget.WebDialog;
import com.quickblox.q_municate.R;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class FacebookHelper {

    public static final List<String> PERMISSIONS = Arrays.asList("publish_actions", "publish_stream");
    private Activity activity;
    private Session.StatusCallback facebookStatusCallback;
    private Resources resources;

    public FacebookHelper(Activity activity, Bundle savedInstanceState,
            Session.StatusCallback facebookStatusCallback) {
        this.activity = activity;
        this.facebookStatusCallback = facebookStatusCallback;
        resources = activity.getResources();
        initFacebook(savedInstanceState);
    }

    public static void logout() {
        if (Session.getActiveSession() != null) {
            Session.getActiveSession().closeAndClearTokenInformation();
        }
    }

    private void initFacebook(Bundle savedInstanceState) {

        Session session = Session.getActiveSession();
        if (session != null) {
            return;
        }
        if (savedInstanceState != null) {
            session = Session.restoreSession(activity, null, facebookStatusCallback, savedInstanceState);
        }
        if (session == null) {
            session = new Session(activity);
        }
        Session.setActiveSession(session);
        if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
            session.openForRead(new Session.OpenRequest(activity).setCallback(facebookStatusCallback));
        }
    }

    public void onSaveInstanceState(Bundle outState) {
        Session session = Session.getActiveSession();
        Session.saveSession(session, outState);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Session.getActiveSession().onActivityResult(activity, requestCode, resultCode, data);
    }

    public void loginWithFacebook() {
        TipsManager.setIsJustLogined(true);
        Session session = Session.getActiveSession();
        if (!session.isOpened() && !session.isClosed()) {
            session.openForRead(new Session.OpenRequest(activity).setCallback(facebookStatusCallback));
        } else {
            Session.openActiveSession(activity, true, facebookStatusCallback);
        }
    }

    public void onActivityStart() {
        Session.getActiveSession().addCallback(facebookStatusCallback);
    }

    public void onActivityStop() {
        if (Session.getActiveSession() != null) {
            Session.getActiveSession().removeCallback(facebookStatusCallback);
        }
    }

    public boolean isSessionOpened() {
        return Session.getActiveSession().isOpened();
    }

    public void postInviteToWall(Request.Callback requestCallback, String[] selectedFriends) {
        Session session = Session.getActiveSession();
        if (session != null) {
            Resources resources = activity.getResources();
            Bundle postParams = new Bundle();
            //Data from string.xml to post on wall ...You have to include the permission for posting
            postParams.putString(Consts.FB_WALL_PARAM_NAME, resources.getString(R.string.inf_fb_wall_param_name));
            postParams.putString(Consts.FB_WALL_PARAM_DESCRIPTION, resources.getString(R.string.inf_fb_wall_param_description));
            postParams.putString(Consts.FB_WALL_PARAM_LINK, resources.getString(R.string.inf_fb_wall_param_link));
            postParams.putString(Consts.FB_WALL_PARAM_PICTURE, resources.getString(R.string.inf_fb_wall_param_picture));
            postParams.putString(Consts.FB_WALL_PARAM_PLACE, resources.getString(R.string.inf_fb_wall_param_place));
            postParams.putString(Consts.FB_WALL_PARAM_TAGS, TextUtils.join(",", selectedFriends));
            Request request = new Request(session, Consts.FB_WALL_PARAM_FEED, postParams, HttpMethod.POST, requestCallback);
            RequestAsyncTask task = new RequestAsyncTask(request);
            task.execute();
        }
    }

    private Bundle getBundleForFriendsRequest() {
        Bundle postParams = new Bundle();
        postParams.putString(Consts.FB_REQUEST_PARAM_TITLE, resources.getString(R.string.inf_subject_of_invitation));
        postParams.putString(Consts.FB_REQUEST_PARAM_MESSAGE, resources.getString(R.string.inf_body_of_invitation));
        return postParams;
    }

    public WebDialog getWebDialogRequest() {
        Bundle postParams = getBundleForFriendsRequest();
        return (new WebDialog.RequestsDialogBuilder(activity,
                Session.getActiveSession(), postParams)).setOnCompleteListener(
                getWebDialogOnCompleteListener()).build();
    }

    private WebDialog.OnCompleteListener getWebDialogOnCompleteListener() {
        return new WebDialog.OnCompleteListener() {

            @Override
            public void onComplete(Bundle values, FacebookException facebookException) {
                parseFacebookRequestError(values, facebookException);
            }
        };
    }

    private void parseFacebookRequestError(Bundle values, FacebookException facebookException) {
        if (facebookException != null) {
            if (facebookException instanceof FacebookOperationCanceledException) {
                DialogUtils.showLong(activity, resources.getString(R.string.inf_fb_request_canceled));
            } else if (facebookException instanceof FacebookServiceException) {
                final int errorCodeCancel = 4201;
                FacebookServiceException facebookServiceException = (FacebookServiceException) facebookException;
                if (errorCodeCancel == facebookServiceException.getRequestError().getErrorCode()) {
                    DialogUtils.showLong(activity, resources.getString(R.string.inf_fb_request_canceled));
                } else {
                    ErrorUtils.showError(activity,
                            facebookServiceException.getRequestError().getErrorMessage());
                }
            } else if (!TextUtils.isEmpty(facebookException.getMessage())) {
                ErrorUtils.showError(activity, facebookException);
            }
        } else {
            final String requestId = values.getString("request");
            if (requestId != null) {
                DialogUtils.showLong(activity, resources.getString(R.string.dlg_success_request_facebook));
            } else {
                DialogUtils.showLong(activity, resources.getString(R.string.inf_fb_request_canceled));
            }
        }
    }

    public boolean checkPermissions() {
        Session session = Session.getActiveSession();
        List<String> permissions = session.getPermissions();
        if (!isSubsetOf(FacebookHelper.PERMISSIONS, permissions)) {
            Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(activity,
                    FacebookHelper.PERMISSIONS);
            session.requestNewPublishPermissions(newPermissionsRequest);
            return false;
        }
        return true;
    }

    private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
        for (String string : subset) {
            if (!superset.contains(string)) {
                return false;
            }
        }
        return true;
    }
}

You can also get this class from the gist link https://gist.github.com/LPURockers/e9cba51d85ffdf077062 您也可以从要点链接https://gist.github.com/LPURockers/e9cba51d85ffdf077062获取此类

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

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