1. 集成Line登录
官网开发文档
- 导入依赖
implementation 'com.linecorp:linesdk:5.0.1'
allprojects {
repositories {
jcenter()
......
}
}
android {
defaultConfig {
......
minSdkVersion 17
......
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
集成登录功能
先验证token是否有效,token有效则可以使用LineApiClient 获取token和userId;token无效需要跳转到Line app登录(如果没安装Line app则打开浏览器进行网页授权) token有效状态下,用户信息都可以通过LineApiClient获取。 LineApiClient 的方法因为需要访问网络,所以都需要在子线程调用。
初始化
public LineApiClient getLineApiClient(Context context) {
if (lineApiClient == null) {
synchronized (appActivity) {
if (lineApiClient == null) {
LineApiClientBuilder apiClientBuilder = new LineApiClientBuilder(context,"你的id");
lineApiClient = apiClientBuilder.build();
}
}
}
return lineApiClient;
}
登陆
public static void loginLine() {
Log.e("TAG", "loginLine: 登陆");
AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> {
try {
if (appActivity.getLineApiClient(appActivity).verifyToken().isSuccess()) {
String accessToken = appActivity.getLineApiClient(appActivity).getCurrentAccessToken().getResponseData().getTokenString();
LineProfile profile = appActivity.getLineApiClient(appActivity).getProfile().getResponseData();
String userId = profile.getUserId();
Log.e("TAG", "loginLine: 登陆 accessToken:" +accessToken +"userId:"+userId );
appActivity.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxJavascriptJavaBridge.evalString("SystemBridge.OnLoginSuccess(\""+userId + "\",\"" + accessToken+"\")");
}
});
return;
}
} catch (Exception e) {
e.printStackTrace();
}
try{
LineAuthenticationParams params = new LineAuthenticationParams.Builder().scopes(Arrays.asList(Scope.PROFILE)).build();
Intent loginIntent = LineLoginApi.getLoginIntent(appActivity, "你的id", params);
appActivity.startActivityForResult(loginIntent, 1000);
} catch(Exception e) {
e.printStackTrace();
}
});
}
回调
private void handleLineInResult(LineLoginResult result) {
Log.e("TAG", "handleLineInResult:result:" +result.getResponseCode());
switch (result.getResponseCode()) {
case SUCCESS:
try {
String accessToken = result.getLineCredential().getAccessToken().getTokenString();
String userId = result.getLineProfile().getUserId();
String displayName = result.getLineProfile().getDisplayName();
Log.e("TAG", "handleLineInResult: 登陆 accessToken:" +accessToken +"userId:"+userId );
appActivity.runOnGLThread(new Runnable() {
@Override
public void run() {
Cocos2dxJavascriptJavaBridge.evalString("SystemBridge.OnLoginSuccess(\""+userId + "\",\"" + accessToken+"\")");
}
});
} catch (Exception e) {
e.printStackTrace();
}
break;
case CANCEL:
break;
default:
}
}
重载onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
SDKWrapper.getInstance().onActivityResult(requestCode, resultCode, data);
if(requestCode == 1000){
LineLoginResult result = LineLoginApi.getLoginResultFromIntent(data);
handleLineInResult(result);
}
}
基本就这么多。
|