简体   繁体   中英

Manually build a login flow for Facebook on Android

I'm working on a project where I need to implement signing in using Facebook authorization on android. I've implemented Facebook API but it provides access token, when I need code (which is used to get token). I found advises saying that I can't use/modify FB api to just get code, instead I have to program my own login flow. I know there is basic documentation on fb developer page but it doesn't say anything about implementing this function on android.

Any help would be much appreciated.

Everything you need is on the official Facebook page.

Manually building a login flow: https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow

Android SDK: https://developers.facebook.com/docs/android/

Android SDK source code: https://github.com/facebook/facebook-android-sdk

I've found an answer. As I thought, simplest way to do it is by using retrofit. -> Integrate OAuth in Your App.

Code snippet:

// you should either define client id and secret as constants or in string resources
private final String clientId = "xxxxxxxxxxxxxxxxx";
private final String responseType = "code";

/**
 * same as in manifest in intent filter
 */
private final String redirectUri = "http://www.example.com/gizmos";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "xxxxxxxxxxxxxxx",  // replace with your unique package name
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

    Button loginButton = (Button) findViewById(R.id.loginbutton);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse(ServiceGenerator.API_BASE_URL + "/dialog/oauth" +
                            "?client_id=" + clientId +
                            "&redirect_uri=" + redirectUri +
                            "&response_type=" + responseType));
            startActivity(intent);
        }
    });
}

@Override
protected void onResume() {
    super.onResume();

    // the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent
    Uri uri = getIntent().getData();
    if (uri != null && uri.toString().startsWith(redirectUri)) {
        // use the parameter your API exposes for the code (mostly it's "code")
        String code = uri.getQueryParameter("code");
        if (code != null) {
            Log.i("code", code);
            // get access token
            // we'll do that in a minute
        } else if (uri.getQueryParameter("error") != null) {
            // show an error message here
        }
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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