1

所以,这是我的登录功能:

  public void facebookLogin()
{

    shareAppPreferences = new AppPreferences(getApplicationContext());
     facebook = ((GlobalVars)getApplicationContext()).facebook;

     String access_token = shareAppPreferences.getAccessToken();
     long expires = shareAppPreferences.getAccessExpires();
     Log.d("MyTag","token:" +access_token);
     if(access_token != null) {
         facebook.setAccessToken(access_token);
     }
     if(expires != 0) {
         facebook.setAccessExpires(expires);
     }
     /*
      * Only call authorize if the access_token has expired.
      */
     if(!facebook.isSessionValid()) {
         Log.d("MyTag","In Authorize");
          facebook.authorize(this, new String[] {}, new DialogListener() {
              @Override
              public void onComplete(Bundle values) {
                //  new AccessKeyTask().execute("http://37.49.226.66/allaroundme/requests.php");
                  shareAppPreferences.saveAccessToken(facebook.getAccessToken());
                  Log.d("MyTag","face token: " +facebook.getAccessToken());
                  shareAppPreferences.saveAccessExpires(facebook.getAccessExpires());

              }

              @Override
              public void onFacebookError(FacebookError error) {}

              @Override
              public void onError(DialogError e) {}

              @Override
              public void onCancel() {}
          });
     }
     Intent intent = new Intent(this,MainActivity.class);
     Log.d("MyTag", "starting activity");
     startActivity(intent);

}

我真的不明白为什么这段代码不起作用,我在尝试使用该代码之前通过 facebook 应用程序注销了 facebook。onComplete() 似乎没有发生,并且访问令牌始终为空。但在我第一次登录后,我不必输入信息。有人能帮我一下吗?

4

1 回答 1

2

这是对我有用的代码:

    public void loginToFacebook(){
        facebook.authorize(CandyCount.this, new String[]{"publish_stream", "email"}, Facebook.FORCE_DIALOG_AUTH, new DialogListener() {

            public void onFacebookError(FacebookError e) {
                Toast.makeText(getApplicationContext(), "onFacebookError", Toast.LENGTH_SHORT).show();
            }

            public void onError(DialogError e) {
                Toast.makeText(getApplicationContext(), "onError", Toast.LENGTH_SHORT).show();
            }

            public void onComplete(Bundle values) {

                Editor editor = sp.edit();
                editor.putString("access_token", facebook.getAccessToken());
                editor.putLong("access_expires", facebook.getAccessExpires());
                editor.commit();
                updateButtonStatus();

                getName();

            }

            public void onCancel() {
                // TODO Auto-generated method stub

            }

        });
    }

在 onCreate 中:

    sp = getPreferences(MODE_PRIVATE);
    access_token = sp.getString("access_token", null);
    expires = sp.getLong("access_expires", 0);
    getName = sp.getString("name", null);

    if (access_token != null){
        facebook.setAccessToken(access_token);
        fbName.setText("Hello, " + getName);
        updateButtonStatus();
    }
    if (expires != 0){
        facebook.setAccessExpires(expires);

    }

在 updateButtonStatus 方法中,我更改了按钮的图像,在按钮的 onClick 中,我根据会话是否有效退出或登录。

查看此链接,它在解释 facebook sdk 方面非常好。

您显然需要更改代码以满足您的特定需求。我希望这有帮助!

于 2012-11-04T00:28:51.197 回答