2

我正在尝试使用最新的 FB Sdk 登录 FB。

这是我的代码

Session currentSession = Session.getActiveSession();
if (currentSession == null || currentSession.getState().isClosed()) {
    Session session = new Session.Builder(v.getContext())
            .setApplicationId(getResources().getString(R.string.app_Id))
            .build();
    Session.setActiveSession(session);
    currentSession = session;
}
if (!currentSession.isOpened()) {
    Session.OpenRequest openRequest = null;
    openRequest = new Session.OpenRequest(FBLogin.this);
    if (openRequest != null) {
        openRequest.setDefaultAudience(SessionDefaultAudience.EVERYONE);
        openRequest.setPermissions(PERMISSIONS);
        openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
        currentSession.addCallback(callback);
        currentSession.openForRead(openRequest);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Constants.RESULT_OK) {
        finish();
    } else Session.getActiveSession().onActivityResult(this, requestCode,
        resultCode, data);
}

private Session.StatusCallback callback = new Session.StatusCallback() {

    public void call(Session session, SessionState state,
            Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

private void onSessionStateChange(Session session, SessionState state,
        Exception exception) {
    if (state.isOpened()) {
        registerWithServer(session);
    } else if (state.isClosed()) {
        mDialog.dismiss();
        Utilities
                .displayAlert(
                    "Oops!",
                    "Could not connect to Facebook. Try removing application"
                    + "from App dashboard", FBLogin.this);
    }
}

代码在您第一次尝试运行时运行良好。但如果应用程序被卸载或重新安装,然后您尝试登录,它会打开 FB 页面,请求权限,然后给出FacebookException无效令牌。

我通过“尝试从应用仪表板中删除应用程序”修复它的唯一方法

到底是怎么回事?FB 代币如何运作?在重新授权时,它应该创建一个新令牌而不是失败,对吗?

4

1 回答 1

0
          Try this code for facebook login

       Session.openActiveSession(this, true, new Session.StatusCallback() {

      // callback when session changes state
      @Override
      public void call(final Session session, SessionState state, Exception exception) {

        if (session.isOpened()) {
            if(!session.getPermissions().contains("email"))
                    {
                session.requestNewReadPermissions(new Session.NewPermissionsRequest(Facebookpersil.this, PERMISSIONS));
                    }

          // make request to the /me API
          Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

            // callback after Graph API response with user object
            @Override
            public void onCompleted(GraphUser user, Response response) {
              if (user != null) {

                  try
                  {

                Log.w("",""+user.asMap());
               name= user.getName();
            //   location=user.getLocation().getCity();
               nickname=user.asMap().get("username").toString();
               email=user.asMap().get("email").toString();
                String id=user.getId();
                URL img_value = null;
                try {
                    img_value = new URL("http://graph.facebook.com/"+id+"/picture?type=small");
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                fbprofileimage=img_value.toString();
                session.close();
                if(email.length()==0)
                {
                    Toast("Unexpected error. Please try again");
                    finish();
                }
                else{
                    new Loginauth().execute();
                }
                  }
                  catch(Exception e)
                  {
                      Log.w("error", e.toString());
                  }
                }
            }
          });
        }
      }
    });




  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
  }
于 2013-07-25T09:54:02.023 回答