没有登录按钮的 Facebook 身份验证 [英] Facebook authentication without login button
问题描述
我遵循了一些 Facebook API 3.0 教程,包括登录/注销和发布到供稿示例.所以登录是这样工作的:
I have followed some Facebook API 3.0 tutorials, including the Login/Logout and the Publish To Feed examples. So the login works this way:
- 应用打开,显示一个显示登录按钮的片段
- 用户点击登录,通过引用的 FacebookSDK 库 (com.facebook.widget.LoginActivity) 和使用会话提供的代码完成身份验证.
- 用户被重定向到下一个屏幕
我不想让用户以这种方式登录.我希望他们在没有任何登录/注册的情况下使用我的应用程序,然后如果他们点击 Facebook 特定功能,例如在 Facebook 上分享便条,然后应用程序应该询问他们是否让 Facebook 使用他们的应用程序或其他什么,你知道常见的东西.如果没有这个,我会在 publishFeedDialog() 函数中得到一个空指针,因为会话为空,因为没有登录.
I don't want to make the user to login this way. I want them to use my app without any login/registration, then if they click on a facebook specific feature e.g. share a note on Facebook, then the app should ask them whether they let Facebook use their app or something, you know the usual stuff. Without this I get a nullpointer in the publishFeedDialog() function as session is null, because no login has been made.
所以我的问题是,如何使用登录按钮忽略 SplashFragment,因此当用户单击我的应用程序中的 Facebook 功能时,不会显示带有登录按钮的新屏幕,而仅显示默认的 Facebook 身份验证窗口用户习惯吗?
So my question is, how can I ignore the SplashFragment with the Login button, so when the user clicks on a Facebook feature in my app, no new screen is displayed with a login button, but only the default Facebook authentication window that users are used to?
推荐答案
@erdomester, @sromku
@erdomester, @sromku
Facebook 推出新的 sdk 版本 4.x,其中 Session 已弃用,
Facebook launch new sdk version 4.x where Session is deprecated,
有来自 facebook 的登录新概念
There new concept of login as from facebook
LoginManager 和 AccessToken - 这些新类执行 Facebook登录
LoginManager and AccessToken - These new classes perform Facebook Login
所以,现在您无需登录按钮即可访问 Facebook 身份验证
So, Now you can access Facebook authentication without login button as
layout.xml
<Button
android:id="@+id/btn_fb_login"
.../>
MainActivity.java
private CallbackManager mCallbackManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(mCallbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("Success", "Login");
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.this, "Login Cancel", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(MainActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
setContentView(R.layout.activity_main);
Button btn_fb_login = (Button)findViewById(R.id.btn_fb_login);
btn_fb_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
}
});
}
编辑
如果您不添加以下内容,它将不起作用(@Daniel Zolnai 在下面的评论中):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(mCallbackManager.onActivityResult(requestCode, resultCode, data)) {
return;
}
}
这篇关于没有登录按钮的 Facebook 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!