Skip to content

Issue with Android getUserId on first login. #160

Description

@varyP

auto facebook = cocos2d::plugin::FacebookAgent::getInstance();
std::string permissions = "public_profile,user_friends";
facebook->login(permissions, [=](int ret, std::string& msg){
CCLOG("type is %d, msg is %s", ret, msg.c_str());

    if (facebook->isLoggedIn()) {

        //Save access token
        auto accessToken = facebook->getAccessToken();

        //Save user id
        auto id = facebook->getUserID();
   }

}


ID here after first login comes to be empty.


Fix :

private class SessionStatusCallback implements Session.StatusCallback {
@OverRide
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
if(false == isLoggedIn){
if(SessionState.OPENED == state){
isLoggedIn = true;
// UserWrapper.onActionResult(mAdapter, UserWrapper.ACTION_RET_LOGIN_SUCCEED, getSessionMessage(session));
}else if(SessionState.CLOSED_LOGIN_FAILED == state /|| SessionState.CLOSED == state/){
UserWrapper.onActionResult(mAdapter, UserWrapper.ACTION_RET_LOGIN_FAILED, getErrorMessage(exception, "login failed"));
}

        }
        else{
            if(SessionState.OPENED_TOKEN_UPDATED == state){
                UserWrapper.onActionResult(mAdapter, UserWrapper.ACTION_RET_LOGIN_SUCCEED, getSessionMessage(session));
            }                   
            else if(SessionState.CLOSED == state || SessionState.CLOSED_LOGIN_FAILED == state){
                isLoggedIn = false;
                UserWrapper.onActionResult(mAdapter, UserWrapper.ACTION_RET_LOGIN_FAILED, getErrorMessage(exception, "failed"));
            }                   
        }
    }
}

private void onSessionStateChange(final Session session, SessionState state,
Exception exception) {
if (session != null && session.isOpened()) {
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
@OverRide
public void onCompleted(GraphUser user,
Response response) {
LogD("User here");
if (user != null) {
LogD("User here " + user.getId());
userIdStr = user.getId();
UserWrapper.onActionResult(mAdapter, UserWrapper.ACTION_RET_LOGIN_SUCCEED, getSessionMessage(session));
}

            }
        }).executeAsync();
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions