我正在开发一个项目,我需要在Android上使用Facebook授权实现登录。我已经实现了Facebook API,但是当我需要代码(用于获取令牌)时,它提供了访问令牌。我发现建议说我不能使用/修改FB api来获取代码,而是我必须编写自己的登录流程。我知道fb开发者页面上有基本的文档,但它没有说明在android上实现这个功能。
非常感谢任何帮助。
答案 0 :(得分:1)
您需要的一切都在官方的Facebook页面上。
手动构建登录流程:https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
Android SDK:https://developers.facebook.com/docs/android/
Android SDK源代码:https://github.com/facebook/facebook-android-sdk
答案 1 :(得分:1)
我找到了答案。我认为,最简单的方法是by using retrofit. - >在您的应用程序中集成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
}
}
}
}