Facebook身份验证无登录按钮 [英] Facebook authentication without login button

查看:193
本文介绍了Facebook身份验证无登录按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了一些Facebook API 3.0教程,包括登录/注销和发布到Feed示例。所以登录工作方式如下:


  1. 应用程序打开,显示一个显示登录按钮的片段

  2. 用户点击登录,通过引用的FacebookSDK库(com.facebook.widget.LoginActivity)和提供的代码使用会话完成身份验证。

  3. 用户被重定向到下一个屏幕

我不想让用户以这种方式登录。我希望他们使用我的应用程序没有任何登录/注册,然后如果他们点击一个Facebook的特定功能,例如在Facebook上分享一个笔记,那么应用程序应该问他们是否让Facebook使用他们的应用程序或某事,你知道通常的东西。没有这个我在publishFeedDialog()函数中获得一个nullpointer,因为session为null,因为没有登录。



所以我的问题是,我如何忽略SplashFragment使用登录按钮,所以当用户点击我的应用程序中的Facebook功能时,没有新的屏幕显示登录按钮,但只有用户用于默认的Facebook身份验证窗口?

解决方案

@erdomester,@sromku



Facebook发布新的sdk版本4.x,其中Session被弃用, / p>

从Facebook登录的新概念


所以,现在你可以访问Facebook身份验证,而不需要登录按钮作为



布局。 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);

按钮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 requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(mCallbackManager.onActivityResult(requestCode,resultCode,data)){
return;
}
}


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:

  1. App opens, shows a fragment which displays a login button
  2. User clicks login, the authentication is done via the referenced FacebookSDK library (com.facebook.widget.LoginActivity) and the provided code using sessions.
  3. User is redirected to next screen

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.

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

Facebook launch new sdk version 4.x where Session is deprecated,

There new concept of login as from facebook

LoginManager and AccessToken - These new classes perform Facebook Login

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"));
        }
    });

}

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屋!

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