---
name: "LWP::Authen::OAuth2::AccessToken"
section: "3pm"
platform: "linux"
package: "liblwp-authen-oauth2-perl"
version: "0.20-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/LWP::Authen::OAuth2::AccessToken"
---

# LWP::Authen::OAuth2::AccessToken(3pm)

Access tokens for OAuth2.

## NAME

LWP::Authen::OAuth2::AccessToken - Access tokens for OAuth2.

## VERSION

version 0.20

## SYNOPSIS

This is a base class for signing API requests with OAuth2 access tokens.  A subclass should override the `"request"` method with something that knows how to make a request, detect the need to try to refresh, and attmpts to do that. See lWP::Authen::OAuth2::AccessToken::Bearer for an example.

Subclasses of this one are not directly useful.  Please see LWP::Authen::OAuth2 for the interface that you should be using.

## METHODS

### "from\_ref"

Construct an access token from a hash reference.  The default implementation merely blesses it as an object, defaulting the `"create_time"` field to the current time.

```
    my $access_token = $class->from_ref($data);
```

If you roll your own, be aware that the fields `"refresh_token"` and `"_class"` get used for purposes out of this class' control.  Any other fields may be used.  Please die fatally if you cannot construct an object.

### "to\_ref"

Construct an unblessed data structure to represent the object that can be serialized as JSON.  The default implementation just creates a shallow copy and assumes there are no blessed subobjects.

### "expires\_time"

Estimate expiration time.  Not always correct, due to transit delays, clock skew, etc.

### "expires\_in"

Estimate the seconds until expiration.  Not always correct, due to transit delays, clock skew, etc.

### "should\_refresh"

Boolean saying whether a refresh should be emitted now.

### "for\_refresh"

Returns key/value pairs for `$oauth2` (and eventually the service provider class) to use in trying to refresh.

### "copy\_refresh\_from"

Pass in a previous access token, copy anything needed to refresh.

### "request"

Make a request.  If expiration is detected, refreshing by the best available method (if any).

```
    my $response = $access_token->request($oauth2, @request_for_lwp);
```

### "\_request"

Make a request with no retry logic, and return a response, and a flag for whether it is possible the access token is expired..

```
    my ($response, $try_refresh)
        = $access_token->_request($oauth2, @request_for_lwp);
```

**THIS IS THE ONLY METHOD A SUBCLASS MUST OVERRIDE!**

## AUTHORS

- •

  Ben Tilly, \<btilly at gmail.com\>

- •

  Thomas Klausner \<domm@plix.at\>

## COPYRIGHT AND LICENSE

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.
