在Android上手动构建Facebook的登录流程 [英] Manually build a login flow for Facebook on Android

查看:130
本文介绍了在Android上手动构建Facebook的登录流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个项目,我需要在Android上使用Facebook授权进行登录。当我需要代码(用于获取令牌)时,我已经实现了Facebook API,但它提供了访问令牌。我发现建议说我不能使用/修改FB api来获取代码,而是我必须编程我自己的登录流程。我知道有关于fb开发者页面的基本文档,但它并没有说明在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.

推荐答案

我找到了一个答案。正如我所想,最简单的方法是使用改装。 - >将OAuth集成到您的应用程序中。

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

代码片段:

// 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
        }
    }
}

}

这篇关于在Android上手动构建Facebook的登录流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆