How to log in with other facebook account?

前端 未结 2 1695
名媛妹妹
名媛妹妹 2021-01-22 06:47

I\'m follow this tutorial to make a small app to login and say hello + user name.

The issue is: I can only login using my account, but can\'t log in with other account.

2条回答
  •  清歌不尽
    2021-01-22 07:06

    This is expected behavior. Essentially the login for facebook is SSO (single sign on) so there is a strong expectation that the user has only one account on their device.

    I myself have tried to find a way to get the Facebook SDK to allow the user to sign on to a different account but it doesn't work.

    It might be possible fudge it by clearing the caches perhaps but this wouldn't help users who are using the genuine facebook app on their phone.

    What I did in the end was went to the web workflow as opposed to native app. I can recommend scribe for this task.

    https://github.com/fernandezpablo85/scribe-java

    If you do choose to use Scribe, this is my activity for loggin in.

    public class FacebookScribeLogin extends FragmentActivity{
    private static final String TAG = FacebookScribeLogin.class.getSimpleName();
    private final static String CALLBACK = "http://localhost:3000/";
    
    private WebView mWebView;
    private ProgressDialog mProgressDialog;
    
    private OAuthService mAuthService;
    private SyncStatusUpdaterFragment mSyncStatusUpdaterFragment;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
    
        mWebView = (WebView) findViewById(R.id.webview);
    
        new GetLoginPage().execute();
    }
    
    private class GetLoginPage extends AsyncTask {
        @Override
        protected String doInBackground(Void... voids) {
            //set up service and get request token as seen on scribe website
            //https://github.com/fernandezpablo85/scribe-java/wiki/Getting-Started
            mAuthService = new ServiceBuilder()
                    .provider(FacebookApi.class)
                    .apiKey(getString(R.string.facebook_api_key))
                    .apiSecret(getString(R.string.facebook_api_secret))
                    .scope("read_stream, publish_stream, manage_notifications, publish_actions, manage_pages")
                    .callback(CALLBACK)
                    .build();
    
            return mAuthService.getAuthorizationUrl(null);
        }
    
        @Override
        protected void onPostExecute(String authURL) {
            //send user to authorization page
            android.webkit.CookieManager.getInstance().removeAllCookie();
    
            mWebView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    //check for our custom callback protocol otherwise use default behavior
                    if (url.startsWith(CALLBACK)) {
                        GetAccessToken getAccessToken = new GetAccessToken(url);
                        getAccessToken.execute();
    
                        return true;
                    }
    
                    if(mProgressDialog == null){
                        mProgressDialog = ProgressDialog.show(FacebookScribeLogin.this, null,
                            String.format(getString(R.string.talking_to_x), getString(R.string.facebook)), true, false);
                    }
    
                    return super.shouldOverrideUrlLoading(view, url);
                }
    
                @Override
                public void onPageFinished(WebView view, String url) {
                    if(mProgressDialog != null){
                        mProgressDialog.hide();
                        mProgressDialog = null;
                    }
                }
            });
            mWebView.loadUrl(authURL);
    
        }
    }
    
    private class GetAccessToken extends AsyncTask{
        private String mUrl, mToken, mSecret;
    
        private GetAccessToken(String url) {
            mUrl = url;
        }
    
        @Override
        protected Void doInBackground(Void... voids) {
            Uri uri = Uri.parse(mUrl);
    
            String verifierStr = uri.getQueryParameter("code");
            Verifier verifier = new Verifier(verifierStr);
    
            //save this token for practical use.
            Token accessToken = mAuthService.getAccessToken(null, verifier);
            mToken = accessToken.getToken();
            mSecret = accessToken.getSecret();
    
            return null;
        }
    
        @Override
        protected void onPostExecute(Void s) {
            //mToken - save your mToken somehwere and perhaps use a graph API call for user details
        }
    }
    
    
    
    }
    

提交回复
热议问题