没有登录按钮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),并利用会议提供的code完成。
- 在用户被重定向到下一个屏幕
我不想让登录这样的用户。我希望他们使用我的应用程序没有任何登录/注册,那么如果他们点击一个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版本,其中会话是德precated,
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"));
}
});
}
修改
如果您不添加以下,这是不行的(正确指出的<一个href="http://stackoverflow.com/questions/18135885/facebook-authentication-without-login-button#comment48833948_30230718">@Daniel Zolnai 在下面的评论):
Edit
If you don't add the following, it won't work (rightly pointed out by @Daniel Zolnai in comment below):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(mCallbackManager.onActivityResult(requestCode, resultCode, data)) {
return;
}
}
这篇关于没有登录按钮Facebook的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!