Net::O...sToken(3pm)
πΈ NAME πΈ
Net::OAuth2::AccessToken β an OAuth2 bearer token, bestie. That's the whole thing. π
(Also hi, you're on the User Contributed Perl Documentation page β the one your terminal labels Net::O...sToken(3pm) up top. It's giving mysterious acronym energy but it's literally just this module. π¦)
π SYNOPSIS π
my $auth = Net::OAuth2::Profile::WebServer->new(...);
my $session = $auth->get_access_token($code, ...);
# $session is a Net::OAuth2::AccessToken object
if($session->error)
{ print $session->error_description;
}
my $response = $session->get($request);
my $response = $session->get($header, $content);
print $session->to_string; # JSON
# probably better to set new(auto_refresh), but you may do:
$session->refresh if $session->expired;
π DESCRIPTION π
So this object represents a received (bearer) token, and it hands you the tools to actually use it and maintain it. Ngl, a better name for this module would've included client or session, but we move. π
A "bearer token" is basically an abstract proof of your existence: different services β or even potentially different physical servers β can exchange information about your session based on it. Like, for instance, whether someone logged-in while showing the token. It's lowkey a β¨hall passβ¨ for the internet. Periodt.
π METHODS π
π§ Constructors π§
Net::OAuth2::AccessToken->new(%options)
Here's the whole defaults roster, verbatim, no cap:
-Option --Default
access_token undef
auto_refresh <false>
changed <false>
error undef
error_description <value of error>
error_uri undef
expires_at undef
expires_in undef
profile <required>
refresh_always BOOLEAN
refresh_token false
scope undef
token_type undef
And now, one by one, because you deserve the details:
access_token => STRINGβ the token itself, bestie.auto_refresh => BOOLEANβ refresh the token when it's expired. Set it, honestly. π«Άchanged => BOOLEANβ [0.52] the token (session) needs to be saved.error => STRINGβ set when an error has occurred and the token is not valid. This one is not numerical, so don't go treating it like a number. πerror_description => STRINGβ a humanly readible explanation on the error. Heads up: this defaults to the string set with the"error"option, which is not nice to read. πerror_uri => URIβ where to go find more details about the error.expires_at => TIMESTAMPβ expire this token after TIMESTAMP (as produced by thetime()function).expires_in => SECONDSβ expire the token SECONDS after the initiation of this object.profile => Net::OAuth2::Profile objectβ your profile object, required btw.refresh_always => BOOLEANβ [0.53] auto-refresh the token at each use. Every single time. Iconic, if a lil extra.refresh_token => STRINGβ [0.53] token which can be used to refresh the token, after it has expired or earlier.scope => URLβ the scope, as a URL.token_type => TYPEβ what type of token this is.
Net::OAuth2::AccessToken->session_thaw($session, %options)
Pass in the output of a session_freeze() call from the past (maybe even from an older version of this module β she's flexible π«) and you get the token object revived. This $session is a HASH.
You may pass any of the parameters for new() as %options, to overrule the values inside the $session. Overruling? In this economy? Slay.
-Option --Default
profile <required>
profile => Net::OAuth2::Profile objectβ required, as ever.
example:
my $auth = Net::OAuth2::Profile::WebServer->new(...);
my $token = $auth->get_access_token(...);
my $session = $token->session_freeze;
# now save $session in database or file
...
# restore session
my $auth = Net::OAuth2::Profile::WebServer->new(...);
my $token = Net::OAuth2::AccessToken->session_thaw($session
, profile => $auth);
β¨ Accessors β¨
$obj->access_token()β returns the (base64 encoded version of the) access token. The token will get updated first, if it has expired andrefresh_tokenis enabled, or whennew(auto_refresh)is set. And listen: it does not matter that the token is base64 encoded or not β it will always need to be base64 encoded during transport. π$obj->attribute(NAME)β [0.58] sometimes the token gets attributes which are not standard; they have no official accessor (yet?). You can get them with this generic accessor. Very iykyk of it.$obj->changed( [BOOLEAN] )β [0.52] the session (token) needs to be saved, because any of the crucial parameters have been modified and"auto_save"is not defined by the profile.$obj->hd()β getter, no extra notes, she's just here. πΈ$obj->profile()β your profile.$obj->scope()β your scope.$obj->state()β your state.$obj->token_type()β your token type.
errors
When the token is received (hence this object created) it be the result of an error. It is the way the original code was designed... π (we don't make the rules, we just inherit them)
$obj->error()β the error.$obj->error_description()β the description of said error.$obj->error_uri()β where to read more about it.
Expiration
$obj->auto_refresh()β the auto-refresh flag.$obj->expired( [$after] )β returns true when the token has an expiration set and that time has passed. We use this token $after this check: to avoid the token timing out inbetween, we take a margin of (by default) 15 seconds. Safety first, bestie. β°$obj->expires_at( [$timestamp] )β returns the expiration timestamp of this token (true) or"undef"(false) when it is not set.$obj->expires_in()β returns the number of seconds left before the token is expired. That may be negative, so don't panic when it is.$obj->refresh_always()β the refresh-always flag.$obj->refresh_token()β the refresh token.$obj->update_token( $token, $tokentype, $expires_at, [$refresh_token] )β change the token. Glow-up, but for tokens.
π₯ Actions π₯
$obj->refresh()β refresh the token, even if it has not expired yet. Returned is the newaccess_tokenvalue, which may beundefon failure. π¬$obj->session_freeze(%options)β this returns aSESSION(a flatHASH) containing all token parameters which needs to be saved to be able to restore this token later. ThisSESSIONcan be passed tosession_thaw()to get revived. The"changed"flag will be cleared by this method.
Be sure that your storage is character-set aware. For instance, you probably want to set 'mysql_enable_utf8' when you store this in a MySQL database. Perl's JSON module will output utf8 by default. π§
$obj->to_json()β freeze this object into JSON. The JSON syntax is also used by the OAuth2 protocol, so a logical choice to provide. However, generically, thesession_freeze()method provided. (Yes, that sentence is a lil quirky. We keep it exactly as it is. π )
HTTP
The token can be encoded in transport protocol in different ways. Using these methods will add the token to the HTTP messages sent. Automatic, bestie. β¨
$obj->delete( $uri, [$header, [$content]] )β DELETE, token attached.$obj->get( $uri, [$header, [$content]] )β GET, token attached.$obj->post( $uri, [$header, [$content]] )β POST, token attached.$obj->put( $uri, [$header, [$content]] )β PUT, token attached.$obj->request($request)β send the request object as-is, token attached.
π COPYRIGHTS π
Copyrights 2013-2019 on the perl code and the related documentation by [Mark Overmeer <markov@cpan.org>] for SURFnet bv, The Netherlands. For other contributors see "Changes".
Copyrights 2011-2012 by Keith Grennan.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://dev.perl.org/licenses/
okay that's the whole page, you read it all, I'm so proud of you π₯Ήπ This one shipped as perl v5.32.1, dated 2021-10-15, and yes the footer still whispers Net::O...sToken(3pm) at you like a lil ghost. bye bestie, go refresh a token responsibly! ππ¦π
π¦ where this page comes from
- debian bookworm: libnet-oauth2-perl 0.67-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libnet-oauth2-perl 0.67-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libnet-oauth2-perl 0.67-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libnet-oauth2-perl 0.67-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libnet-oauth2-perl 0.67-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libnet-oauth2-perl 0.67-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms