Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android上手动构建Facebook的登录流_Android_Facebook_Api_Oauth_Token - Fatal编程技术网

在Android上手动构建Facebook的登录流

在Android上手动构建Facebook的登录流,android,facebook,api,oauth,token,Android,Facebook,Api,Oauth,Token,我正在做一个项目,需要在android上使用Facebook授权实现登录。我已经实现了FacebookAPI,但当我需要代码(用于获取令牌)时,它提供了访问令牌。我发现有建议说,我不能使用/修改FBAPI来获取代码,相反,我必须编写自己的登录流。我知道fb开发者页面上有一些基本的文档,但没有说明在android上实现这个功能 任何帮助都将不胜感激 你需要的一切都在Facebook的官方页面上 手动构建登录流: Android SDK: Android SDK源代码:您需要的一切都在Faceboo

我正在做一个项目,需要在android上使用Facebook授权实现登录。我已经实现了FacebookAPI,但当我需要代码(用于获取令牌)时,它提供了访问令牌。我发现有建议说,我不能使用/修改FBAPI来获取代码,相反,我必须编写自己的登录流。我知道fb开发者页面上有一些基本的文档,但没有说明在android上实现这个功能


任何帮助都将不胜感激

你需要的一切都在Facebook的官方页面上

手动构建登录流:

Android SDK:


Android SDK源代码:

您需要的一切都在Facebook的官方页面上

手动构建登录流:

Android SDK:


Android SDK源代码:

我找到了答案。正如我所想,最简单的方法是->在应用程序中集成OAuth

代码段:

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

}我找到了答案。正如我所想,最简单的方法是->在应用程序中集成OAuth

代码段:

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

}

我已经编写并实现了该API,但手动登录流没有使用它(如果我理解正确的话)。你刚才给了我很多我已经看过的链接。我的问题是我必须使用什么来实现这个登录流。用OAuth进行改造?修改facebook API?一些okHttp?@Reiz3N您不需要使用任何库(如果您愿意,您可以)。只是收到请求而已。请参阅@Reiz3N,为什么不使用官方SDK?“?client_id={app id}&redirect_uri={redirect uri}”不是一个带有www表单urlencoded的POST方法吗?正如我所写的,当使用官方SDK时,您得到的是令牌而不是代码。正如这里所说:使用官方SDK方法是不允许的。@Reiz3N不,不是。您(以编程方式)在浏览器中打开该URL。我已经编写并实现了该API,但手动登录流没有使用它(如果我理解正确的话)。你刚才给了我很多我已经看过的链接。我的问题是我必须使用什么来实现这个登录流。用OAuth进行改造?修改facebook API?一些okHttp?@Reiz3N您不需要使用任何库(如果您愿意,您可以)。只是收到请求而已。请参阅@Reiz3N,为什么不使用官方SDK?“?client_id={app id}&redirect_uri={redirect uri}”不是一个带有www表单urlencoded的POST方法吗?正如我所写的,当使用官方SDK时,您得到的是令牌而不是代码。正如这里所说:使用官方SDK方法是不允许的。@Reiz3N不,不是。您(以编程方式)在浏览器中打开该URL。您好,您能解释一下重定向uri吗?您所说的:与意图过滤器中的清单相同(这需要是一个url?)是什么意思?谢谢。您好,您能解释一下重定向uri吗?您所说的:与意图过滤器中的清单相同(这需要是一个url?)谢谢。