Line授权登录及分享

5,778 阅读1分钟

Line登陆授权

官方文档
developers.line.biz/en/docs/and…
自定义登陆按钮
developers.line.biz/en/docs/and…

集成Line SDK
repositories {
   ...
   jcenter()
}
dependencies {
    ...
    implementation 'com.linecorp:linesdk:5.0.1'
    ...
} 
java版本
android {
  compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
配置

响应点击
/*吊起登录*/
fun login(activity: Activity) {
    Thread(Runnable {
        try {
            // 调起line app授权登录
            var params = LineAuthenticationParams.Builder().scopes(Arrays.asList(Scope.PROFILE)).build()
            var loginIntent = LineLoginApi.getLoginIntent(activity, "channel id", params)
//            var loginIntent = LineLoginApi.getLoginIntentWithoutLineAppAuth(activity, "channel id", params)
            activity.startActivityForResult(loginIntent, REQUEST_CODE_LINE)
        } catch (e: Exception) {
            Log.e("ljwx-line-error-", e.toString())
        }
    }).start()
}
授权结果
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == LoginLine.REQUEST_CODE_LINE) {
            handleResult(data, object : ThirdLoginCallback {
                override fun success(info: ThirdLoginSuccessInfo?) {
                    info?.let {
                        loginThird(2, info.userID, info.diaplayName, info.avater)
                    }
                }

                override fun fail(info: ThirdLoginFailInfo?) {
                    context!!.toast(info?.message!!)
                }
            })
        }
}

/*处理结果*/
    fun handleResult(data: Intent?, callback: ThirdLoginCallback) {
        var result = LineLoginApi.getLoginResultFromIntent(data)
        when (result.responseCode) {
            LineApiResponseCode.SUCCESS -> {
                try {
                    var userId = result.lineProfile!!.userId
                    var displayName = result.lineProfile!!.displayName
                    val successInfo = ThirdLoginSuccessInfo()
                    successInfo.userID = userId
                    successInfo.diaplayName = displayName
                    callback.success(successInfo)
                } catch (e: Exception) {
                    e.printStackTrace()
                    Log.e("ljwx-line-exception-", e.toString())
                }
            }
            LineLoginResult.CANCEL -> {
                Log.e("ljwx-line-cancel-", "cancel")
            }
            else -> {
                val faliInfo = ThirdLoginFailInfo()
                faliInfo.message = result.errorData.message
                callback.fail(faliInfo)
                Log.e("ljwx-line-fail-", faliInfo.code + ":" + faliInfo.message)
            }
        }
    }

特别提醒,这里要改为 published 状态,否则无法授权成功

Line分享

try {
    val scheme = "line://msg/text/" + data.share_url + "/n" + data.share_title
    val uri = Uri.parse(scheme)
    startActivity(Intent(Intent.ACTION_VIEW, uri))
} catch (e: Exception) {

}