0

我正在开发一个应用程序,它使用需要很少的 facebook 信息来注册我自己的网站。

起初我使用的是 facebook SDK 1.5,一切正常。直到 facebook 表示自 2013 年 6 月起他们将停止支持旧的(v2.0 及以下)SDK。

所以我必须将它升级到 3.0,并重新编写我的代码。

以下是我在阅读了 facebook 的官方教程后所做的:

清单文件

<!-- some stuff -->
<uses-permission android:name="android.permission.INTERNET" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/fbAppId"/>
<activity android:name="com.facebook.LoginActivity" android:label="@string/app_name" />

字符串.xml

<!-- some stuff -->
<string name="fbAppId">1234567890</string> <!-- You do know this ID is fake, right ? -->

代码

public class LoginFragment extends SherlockFragment
{
  private UiLifecycleHelper uiHelper;

  private Session.StatusCallback callback=new Session.StatusCallback()
  {
    @Override
    public void call(final Session session, final SessionState state, final Exception exception)
    {
      onSessionStateChange(session, state, exception);
    }
  };

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
    View view=inflater.inflate(R.layout.login_fragment, container, false);

    LoginButton authButton=(LoginButton)view.findViewById(R.id.btnLogin);
    authButton.setFragment(this);
    authButton.setReadPermissions(Arrays.asList("basic_info", "user_status", "email"));

    return view;
  }

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    uiHelper=new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);
  }

  @Override
  public void onResume()
  {
    super.onResume();

    // For scenarios where the main activity is launched and user
    // session is not null, the session state change notification
    // may not be triggered. Trigger it if it's open/closed.
    Session session=Session.getActiveSession();
    if(session!=null&&(session.isOpened()||session.isClosed()))
    {
      onSessionStateChange(session, session.getState(), null);
    }

    uiHelper.onResume();
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data)
  {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
  }

  @Override
  public void onPause()
  {
    super.onPause();
    uiHelper.onPause();
  }

  @Override
  public void onDestroy()
  {
    super.onDestroy();
    uiHelper.onDestroy();
  }

  @Override
  public void onSaveInstanceState(Bundle outState)
  {
    try
    {
      super.onSaveInstanceState(outState);
      setUserVisibleHint(true);
      uiHelper.onSaveInstanceState(outState);
    }
    catch(Exception e)
    {
      e.printStackTrace();
      Log.w(Constants.TAG, "Error on save instance state, "+e.toString());
    }
  }

  private void onSessionStateChange(Session session, SessionState state, Exception exception)
  {
    if(state.isOpened()) //I don't know why state is always not opened......?
    {
      Request request=Request.newMeRequest(session, new Request.GraphUserCallback()
      {
        @Override
        public void onCompleted(GraphUser user, Response response)
        {
          if(user!=null)
          {
            Log.i(Constants.TAG, "Logged in...");
            Log.d("user fname", user.getFirstName());
            Log.d("user lname", user.getLastName());
            Log.d("user username", user.getUsername());
            Log.d("user email", (String) response.getGraphObject().getProperty("email"));
          }
          else
          {
            Log.i(Constants.TAG, "Logged out...");
          }

        }
      });

      Request.executeBatchAsync(request);
    }
  }
}

当我运行我的应用程序时,我可以看到一个 facebook 登录按钮。

按下它,会出现一个对话框,要求我允许我的 APP 访问 facebook 信息、电子邮件...等。

在我按下是之后,什么也没有发生,甚至在 LogCat 中也没有任何结果......

我如何知道用户实际上已登录 facebook 并访问他们的信息?

4

1 回答 1

0

你应该重写 onActivityResult 方法并调用

authButton.onActivityResult(params);
于 2013-04-25T13:36:25.197 回答