A memo about the procedure for linking the existing user account of the company's service and the account of the LINE user using line-bot-sdk-java. `* Please note that the source in the article does not describe exception handling etc. ``
In Account Linkage Sequence, a document is provided on the procedure for linking accounts. Implement while checking here.
I used the following modules.
<dependency>
<groupId>com.linecorp.bot</groupId>
<artifactId>line-bot-servlet</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>com.linecorp.bot</groupId>
<artifactId>line-bot-model</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>com.linecorp.bot</groupId>
<artifactId>line-bot-parser</artifactId>
<version>4.3.0</version>
</dependency>
When you receive a follow event, proceed by returning a message for cooperation.
1 Call API to issue link token 2 Return link token When you receive the follow event, you will get a link token based on your LINE user ID.
LineMessagingClient client = LineMessagingClient.builder("yourAccessToken").build();
//Obtaining tokens for account linkage
IssueLinkTokenResponse linkTokenRes = client.issueLinkToken(event.getSource().getUserId()).get();
String lineLinkToken = linkTokenRes.getLinkToken();
3 Call API to send linking URL Returns a TemplateMessage to the user so that the login form for their service can be displayed. Pass the link token as a parameter to the login form.
//Returns a message with a button to display the user authentication screen of the company's service
List<Action> actions = Arrays.asList(new URIAction("Work together",
URI.create("https://yourdomain/loginform?linkToken=" + lineLinkToken), null));
String botResponseText = "Please enter the account information that works with LINE in the form.";
TemplateMessage templateMessage = new TemplateMessage("Account linkage",
new ButtonsTemplate(null, "Account linkage", botResponseText, actions));
TextMessage textMessage = new TextMessage("The link can be canceled at any time.");
ReplyMessage rep = new ReplyMessage(event.getReplyToken(), Arrays.asList(templateMessage, textMessage));
// TODO reply message
4 Send linking URL The LINE user receives the TemplateMessage sent in 3.
5 Access linking URL 6 Show login form The LINE user clicks the account link button of the message received in 4, and displays the login form of the company's service.
7 Enter credentials 8 Get user ID and generate nonce Generates a Nonce through the user authentication process of the company's service.
9 Redirect to account link endpoint 10 Access account link endpoint After processing the request in 8, the response will be returned to redirect to the following URL.
https://access.line.me/dialog/bot/accountLink?linkToken=[Link token received in 2]&nonce=[Nonce generated in 8]
11 Send webhook event 12 Link accounts When you receive the account linkage event, the user of your service is determined from the Nonce generated in 8 and linked with the LINE user ID.
LinkContent linkContent = event.getLink();
if (linkContent.getResult() == Result.OK) {
String nonce = linkContent.getNonce();
//User judgment by Nonce generated by TODO's own service
//TODO Save the user ID and line user ID of your company's service as a set
TextMessage textMessage = new TextMessage("LINE cooperation is complete.");
List<Action> actions = Arrays
.asList(new PostbackAction("To release", "unlinkAccount"));
TemplateMessage templateMessage = new TemplateMessage("Account link cancellation",
new ButtonsTemplate(null, "Account link cancellation", "To cancel the account linkage, click the button below.", actions));
ReplyMessage rep = new ReplyMessage(event.getReplyToken(), Arrays.asList(textMessage, templateMessage));
// TODO reply message
}
TextMessage textMessage = new TextMessage("Cooperation failure");
ReplyMessage rep = new ReplyMessage(event.getReplyToken(), Arrays.asList(textMessage));
// TODO reply message
This completes the account linkage between the company's service user and the LINE user. The official documentation is detailed and easy to understand.