Line
Line
Line
Line login
注册账号
shengfanzeng@gmail.com>/zxxx/*#Zfsxx
Trying the sample app
https://developers.line.biz/en/docs/line-login-sdks/android-sdk/try-line-login/
准备工作
- 手机科学上网
- 下载 line apk 并注册登录 (可能需要下载 apk 的方式安装,除非你有 google play)
- 下载官方 demo 体验,官方 demo 提供了测试的 channelId,可以进行测试:Trying the sample app
接入工作
创建应用(Channel)
创建 Provider
到 line 开发者平台https://developers.line.biz/console/,注册账号,并添加 provider,provider 类似组织或公司,一般以公司表示创建,然后在此 provider 下创建一个新的 channel
创建 Channel
channel 可以类比国内其他平台创建的应用,最终会得到一个 Channel ID 即 appID),通过此 channelID 与应用关联。创建时选择类型:LINE Login ,根据提示逐个填入相关资料,这些资料都会在后面 app 调起的授权页面显示,可先尝试填充,后面更改。重点关注 Package names 和 App types 两个选项,App types 选择 web 和 App 两个,即支持跳转 app 登录和跳转网页登录;
- 记住 Channel ID
- Android Package names 必填。应用程序的软件包名称,用于启动 Google Play 商店
- Package signatures 可选。您可以通过在新行中输入每个签名来设置多个签名
- gradlew signingReport
- jadx-gui 查看 APK Signature
- apk 查看
- 将 apk 修改后缀为 .rar 文件后解压
- 进入解压后的 META-INF 目录,该目录下会存在文件 CERT.RSA
- 在该目录下打开 cmd,输入命令 :keytool -printcert -file CERT.RSA 这里将会显示出 MD5 和 SHA1 签名
- Android URL scheme 可选。用于启动您的应用程序的自定义 URL 方案
- 最后记得 publish
添加依赖
1
2
3
4
5
6
7
8
9
10
repositories {
...
mavenCentral()
}
dependencies {
...
implementation 'com.linecorp.linesdk:linesdk:latest.release'
...
}
跳转登录
接入登录官方提供了两种方式,内建登录按钮和自定义登录按钮。
内建登录按钮
官方提供的一个控件,视觉上应该是按照 line 的官方要求定义的,直接封装了登录相关的逻辑,com.linecorp.linesdk.widget.LoginButton,官方文档地址:https://developers.line.biz/en/docs/android-sdk/integrate-line-login/#adding-line-login-button。具体代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// A delegate for delegating the login result to the internal login handler.
private LoginDelegate loginDelegate = LoginDelegate.Factory.create();
LoginButton loginButton = rootView.findViewById(R.id.line_login_btn);
// if the button is inside a Fragment, this function should be called.
loginButton.setFragment(this);
loginButton.setChannelId(channelIdEditText.getText().toString());
// configure whether login process should be done by Line App, or inside WebView.
loginButton.enableLineAppAuthentication(true);
// set up required scopes and nonce.
loginButton.setAuthenticationParams(new LineAuthenticationParams.Builder()
.scopes(Arrays.asList(Scope.PROFILE))
// .nonce("<a randomly-generated string>") // nonce can be used to improve security
.build()
);
loginButton.setLoginDelegate(loginDelegate);
loginButton.addLoginListener(new LoginListener() {
@Override
public void onLoginSuccess(@NonNull LineLoginResult result) {
Toast.makeText(getContext(), "Login success", Toast.LENGTH_SHORT).show();
}
@Override
public void onLoginFailure(@Nullable LineLoginResult result) {
Toast.makeText(getContext(), "Login failure", Toast.LENGTH_SHORT).show();
}
});
自定义
基本都会根据自己的视觉 UI 添加按钮,官方也提供的文档入口:https://developers.line.biz/en/docs/android-sdk/integrate-line-login/#starting-login-activity 直接添加如下事件即可:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
try {
val loginIntent: Intent = LineLoginApi.getLoginIntent(
this,
CHANNEL_ID_JP,
LineAuthenticationParams.Builder()
.scopes(listOf(Scope.PROFILE))
// .botPrompt(LineAuthenticationParams.BotPrompt.normal)
.botPrompt(LineAuthenticationParams.BotPrompt.aggressive)
.build()
)
startActivityForResult(loginIntent, REQUEST_CODE)
} catch (e: Exception) {
e.printStackTrace()
Log.e("hacket", e.toString())
}
接收回调
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode != REQUEST_CODE) {
Log.e("hacket", "onActivityResult Unsupported Request");
return
}
val result = LineLoginApi.getLoginResultFromIntent(data)
when (result.responseCode) {
LineApiResponseCode.SUCCESS -> {
// Login successful
val accessToken = result.lineCredential!!.accessToken.tokenString
val friendshipStatusChanged = result.friendshipStatusChanged
tv_line_login_status.text = "Login Successful\n"
tv_line_login_status.append("${result.lineProfile}\n")
tv_line_login_status.append("${result.lineCredential}\n")
tv_line_login_status.append("${accessToken}\n")
tv_line_login_status.append("friendshipStatusChanged=${friendshipStatusChanged}\n")
Toast.makeText(
this,
"Login Successful:${result.lineProfile?.displayName}",
Toast.LENGTH_SHORT
).show()
//
// val transitionIntent = Intent(
// this,
// LoginPostLoginActivity::class.java
// )
// transitionIntent.putExtra("line_profile", result.lineProfile)
// transitionIntent.putExtra("line_credential", result.lineCredential)
// transitionIntent.putExtra("token", accessToken)
// startActivity(transitionIntent)
}
LineApiResponseCode.CANCEL -> { // Login canceled by user
Log.e("hacket", "LINE Login Canceled by user.")
tv_line_login_status.text = "LINE Login Canceled by user.\n"
}
else -> {
// Login canceled due to other error
tv_line_login_status.text = "Login FAILED! ${result.errorData.toString()}\n"
Log.e("hacket", "Login FAILED!")
Log.e("hacket", result.errorData.toString())
}
}
}
添加 bot 为好友
发送代码:
1
2
3
4
5
6
7
8
9
10
val loginIntent: Intent = LineLoginApi.getLoginIntent(
this,
CHANNEL_ID_JP,
LineAuthenticationParams.Builder()
.scopes(listOf(Scope.PROFILE))
// .botPrompt(LineAuthenticationParams.BotPrompt.normal)
.botPrompt(LineAuthenticationParams.BotPrompt.aggressive)
.build()
)
startActivityForResult(loginIntent, REQUEST_CODE)
- onActivityResult
1
2
3
4
5
6
7
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// ...
LineLoginResult result = LineLoginApi.getLoginResultFromIntent(data);
// 获取和公共号的关系,为true表示添加为好友了
boolean friendshipStatusChanged = result.getFriendshipStatusChanged();
// ...
}
- 在好友列表可以看到
- normal 和 aggressive 区别
- normal:当前页增加一个 item 添加好友
- aggressive:在新的页面添加好友
遇到的问题
注意
- 授权页面点击 “ 取消 “ 或返回时返回的信息中的 code 值是 AUTHENTICATION_AGENT_ERROR,而不是 error,引入在官方代码示例上可以添加此 case 用于判断,为什么不进 CANCEL,目前还没搞明白;
- 包名务必保证正确;
- 目前官方提供的其他如刷新 token 接口官方建议不手动去调用,只要后面有在调用接口,token 会不断刷新续期;
提示:错误,无法正常处理
- 解决 1:检查 line 后台配置,重点看包名是否正确
- 解决 2:检查你的 Channel 有没有 publish
Line 中国区注册不了账号
- 挂科学上网
- 用 indonesia 可以不用手机号注册,邮箱就可以注册
本文由作者按照 CC BY 4.0 进行授权