跳到主要内容

登录

一、登录并回写​

完成初始化后调用 showLogin。SDK 处理登录及授权码换 token,回调直接返回 LoginUser。

登录参数​

参数类型说明
activityActivity当前游戏 Activity
reqSendAuth.Req授权请求,字段见下表
listenerNiuxMobileGameLoginListener必须实现登录结果和退出登录两个回调
req 字段类型说明
scopeString平台分配的完整权限,多个权限用空格分隔
stateString游戏自定义的授权请求标识
responseTypeString固定为 code

调用示例​

以下代码放在游戏 Activity 中,替换 scope 后调用 showGameLogin()。

import com.xovs.common.auth.base.SendAuth;
import com.xunlei.niux.mobilegame.sdk.platform.NiuxMobileGame;
import com.xunlei.niux.mobilegame.sdk.platform.NiuxMobileGameLoginListener;
import com.xunlei.niux.mobilegame.sdk.vo.LoginUser;

private final NiuxMobileGameLoginListener loginListener = new NiuxMobileGameLoginListener() {
@Override
public void onUserLoginResult(boolean isSuccess, int errorCode,
String errorDesc, LoginUser loginUser) {
runOnUiThread(() -> {
if (isSuccess && loginUser != null) {
NiuxMobileGame.getInstance().onLoginResult(true, loginUser, false);
// 建立游戏后端会话,再进入选服/选角页面。
} else {
NiuxMobileGame.getInstance().onLoginResult(false, null, false);
// 展示 errorCode/errorDesc,允许重试。
}
});
}
@Override
public void onUserLogout() {
// 清理游戏会话并返回登录页;不要再次调用 onLogout。
}
};

private void showGameLogin() {
SendAuth.Req req = new SendAuth.Req();
req.scope = "平台分配的完整 scope";
req.state = java.util.UUID.randomUUID().toString();
req.responseType = "code";
NiuxMobileGame.getInstance().showLogin(this, req, loginListener);
}

登录回调​

参数类型说明
isSuccessboolean是否登录成功
errorCodeint错误码
errorDescString错误描述
loginUserLoginUser用户与凭证信息,仅成功时使用
LoginUser 字段类型说明
userIdString迅雷用户 ID,不是角色 ID
userNameString用户昵称,可按需设置
accessTokenString访问凭证
refreshTokenString刷新凭证
tokenTypeString凭证类型,默认 Bearer
expiresInint凭证有效期,单位秒
isAutoLoginboolean是否自动登录

登录回写​

onLoginResult 参数类型说明
successboolean登录成功传 true,失败传 false
loginUserLoginUser成功传 SDK 返回的用户对象,失败传 null
isAutoAuthorboolean普通接入传 false

**成功、失败都要回写,示例已包含此步骤。**保留 SDK 返回的凭证字段;成功后 SDK 自动上报启动并尝试展示悬浮窗,不需重复调用。

需要建立游戏后端会话时,由游戏后端验证 accessToken 与 userId 的对应关系。当前回调不提供授权码 code。使用当前凭证可调用 getLoginUser();收到 onTokenExpired 时处理重新登录。

二、退出与切换账号​

  1. 正在角色会话中时,先调用 quitGame(serverId, userId) 停止心跳,userId 使用迅雷用户 ID。
  2. 调用 NiuxMobileGame.getInstance().onLogout(),清理游戏自身会话。
  3. 切换账号时,再调用登录流程。

onLoginResult(false, ...) 不会清理已有账号;onLogout() 不会替代 quitGame() 停止心跳。角色会话步骤见行为上报。

Activity 销毁时移除监听器:

NiuxMobileGame.getInstance().removeLoginListener(loginListener);