LWP::A...::Line(3pm)

πŸ“¦
version 0.20 Β· 2 versions
  • 0.20 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.18 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

LWP::Authen::OAuth2::ServiceProvider::Line β€” bestie, this is the service provider plug-in that gets you into the Line OAuth2 API v2. It lives in the User Contributed Perl Documentation section of your friendly neighbourhood perl v5.36.0 install, and it's here to make Line auth actually make sense. πŸ¦‹


✨ VERSION ✨

version 0.20 β€” tiny number, huge vibes. πŸ‘‘


🌸 SYNOPSIS 🌸

okay so here's the whole ~journey~, from "hey let's log in" all the way to revoking tokens. Read it like a group chat, top to bottom:

    my $oauth2 = LWP::Authen::OAuth2->new(
        service_provider => 'Line',
        redirect_uri     => 'http://example.com/',
        client_id        => 'line_client_id'      # Retrieved from https://developers.line.me/
        client_secret    => 'line_client_secret'  # Retrieved from https://developers.line.me/
    );

    my $url = $oauth2->authorization_url(state => $state);

    # ... Send user to authorization URL and get authorization $code ...

    $oauth2->request_tokens(code => $code);

    # Simple requests

    # User Info
    my $profile       = $oauth2->make_api_call('profile');
    my $userId        = $profile->{userId};
    my $displayName   = $profile->{displayName};
    my $pictureUrl    = $profile->{pictureUrl};
    my $statusMessage = $profile->{statusMessage};

    # Refresh
    $oauth2->refresh_access_token();

    # More complex requests...

    # Verify
    # Manually send the request using the internal user agent - see explanation in "Line API Documentation" below.
    my $access_token_str = $oauth2->access_token->access_token;
    my $res = $oauth2->user_agent->post($oauth2->api_url_base.'oauth/verify' => { access_token => $access_token_str });
    my $content = eval { decode_json($res->content) };
    my $scope      = $content->{scope};
    my $client_id  = $content->{client_id};
    my $expires_in = $content->{expires_in};

    # Revoke
    # Look up the internal refresh token - see explanation in "Line API Documentation" below.
    my $refresh_token_str = $oauth2->access_token->refresh_token;
    $oauth2->post($oauth2->api_url_base.'oauth/revoke' => { refresh_token => $refresh_token_str });
heads up: the two credential lines in ->new(...) have a comment right after the value and no trailing comma, so if you add more args after them you'll want to fix that up. Copy the example as-is and you're golden. πŸ’…

πŸŽ€ REGISTERING πŸŽ€

so like, individual users need to have an account made with the Line application at <https://line.me/download>. That's step one, no skipping. 🫢

and to actually log in with OAuth2? Users must register their email address. Like, it's required, fr fr. Device-specific instructions live on the Line support site at <https://help.line.me/> if you get lost.

API clients (that's you, dev bestie 😌) can follow the Line Login docs at <https://developers.line.me/line-login/overview> to set up the OAuth2 credentials.


πŸ“± Line API Documentation πŸ“±

Go read the Line Social REST API Reference at <https://devdocs.line.me/en/#how-to-use-the-apis> β€” it's the source of truth, and it's giving ✨documentation✨.

As of writing, there are two simple API calls: profile and refresh. Easy. Iconic. No notes.

There are also verify and revoke endpoints, which β€” plot twist β€” require a bit more work. πŸ™ˆ Let's get into it:

πŸ” verify πŸ”

verify is designed for verifying pre-existing access tokens. Instead of using the Authorization header, this endpoint expects the access token to be form-urlencoded in the request body. Because of this, it's necessary to get access to the internal access token string so you can send it in the request body. The LWP::Authen::OAuth2::ServiceProvider::Line::AccessToken token class used by this service provider provides the access_token accessor for exactly this purpose. πŸ‘‘

The server seems to ignore the Authorization header for this request, so including it is probably not a problem. But if you want to avoid sending the access token in the header, it's necessary to manually construct the request and decode the response.

See "SYNOPSIS" for usage examples. ⬆️

πŸ—‘οΈ revoke πŸ—‘οΈ

revoke requires the refresh token to be form-urlencoded in the request body. Because of this, it's necessary to get access to the internal refresh token string so you can send it in the request body. The LWP::Authen::OAuth2::ServiceProvider::Line::AccessToken token class used by this service provider provides the refresh_token accessor for exactly this purpose. See "SYNOPSYS" for usage examples.

(yes, it says SYNOPSYS in the original β€” I'm keeping it, character for character, because that's the whole bit. πŸŽ€)

⏰ Refresh timing ⏰

Line access tokens can be refreshed at any time up until 10 days after the access token expires. That's the window, bestie β€” respect it.

The LWP::Authen::OAuth2::ServiceProvider::Line::AccessToken token class used by this service provider extends the should_refresh method for this purpose, which causes $oauth2->should_refresh() to return false if this 10-day period has lapsed. So it just... tells you no. Politely. πŸ’€


πŸ‘‘ AUTHOR πŸ‘‘

Adam Millerchip, <adam at millerchip.net>

πŸ‘― AUTHORS πŸ‘―

  • Ben Tilly, <btilly at gmail.com>
  • Thomas Klausner <domm@plix.at>

This software is copyright (c) 2013 - 2022 by Ben Tilly, Rent.com, Thomas Klausner.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. slay. πŸ’–


okay that's the whole page, you made it to the end, I'm so proud of you πŸ₯Ή this was documented for perl v5.36.0 and dated 2023-02-04, and it's filed away as User Contributed Perl Documentation β€” now go get those tokens, main character. πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from