简体   繁体   English

用于Android登录的Facebook SDK

[英]Facebook SDK for android login

I tried to make an application which can connect to facebook using the facebook SDK. 我试图制作一个可以使用facebook SDK连接到facebook的应用程序。 I've already signed up to facebook developers and made all the necessary things. 我已经注册了Facebook开发人员,并做了所有必要的事情。 Here's my code: 这是我的代码:

public class MainActivity extends Activity implements OnClickListener {
Facebook fb;
ImageView logButton;
SharedPreferences sp;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String APP_ID = getString(R.string.app_id);
    fb = new Facebook(APP_ID);

    logButton = (ImageView) findViewById(R.id.login);
    logButton.setOnClickListener(this);
}
@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) 
{
    if(fb.isSessionValid())
    {
        try 
        {
            fb.logout(getApplicationContext());
        } 
        catch (MalformedURLException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
    else
    {
        fb.authorize(MainActivity.this, new String[] {"email"}, Facebook.FORCE_DIALOG_AUTH, new DialogListener() {

            @Override
            public void onFacebookError(FacebookError e) 
            {
                Toast.makeText(MainActivity.this, "facebook error "+e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(DialogError e) 
            {
                Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onComplete(Bundle values) 
            {
                Toast.makeText(MainActivity.this, "done", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancel() 
            {
                Toast.makeText(MainActivity.this, "canceled", Toast.LENGTH_LONG).show();
            }
        });
    }
}
@SuppressWarnings("deprecation")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{   
    Toast.makeText(MainActivity.this, "done1", Toast.LENGTH_LONG).show();
    super.onActivityResult(requestCode, resultCode, data);
    fb.authorizeCallback(requestCode, resultCode, data);
    Toast.makeText(MainActivity.this, "done2", Toast.LENGTH_LONG).show();
}

} }

I used Toast to know what happens. 我用Toast知道会发生什么。 The application doesnt show any of the messages. 该应用程序不显示任何消息。 It doesnt call onComplete and I dont know why. 它不调用onComplete,我也不知道为什么。 The dialog is working and when i click on the login button after entering the email and the passowrd, it comes back to the main screen without doing anything. 该对话框正在运行,当我在输入电子邮件和密码后单击登录按钮时,它将不执行任何操作而返回主屏幕。 Does anyone knows what is the problem? 有谁知道这是什么问题? thanks in advance 提前致谢

It happened to me once, I solved it by doing this: First try to use the code below to print your key hashes, check if the printed key hashes is same as the key hash in you Facebook Developer Dashboard, if they are different, change the one in the dashboard to the printed one. 这一次发生在我身上,我通过执行此操作解决了问题:首先尝试使用下面的代码打印您的密钥哈希,检查打印的密钥哈希是否与您的Facebook Developer Dashboard中的密钥哈希相同,如果不同,请更改仪表板上的那个到打印出来的一个。

try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "your package", 
                PackageManager.GET_SIGNATURES);
        for (android.content.pm.Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK :", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM