forked from telstra/MessagingAPI-perl-sample-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.pm
46 lines (37 loc) · 1.14 KB
/
functions.pm
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
use LWP::UserAgent;
use LWP::ConnCache;
use HTTP::Request;
use File::Basename;
use JSON;
use Storable;
## Create a global UA agent.
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
## Telstra DEV details from https://dev.telstra.com/
my $consumer_key = "";
my $consumer_secret = "";
sub get_token(\%) {
my $hash = shift;
## Check we already have a valid token
if ( $hash->{token} and $hash->{token_expires} > time() ) {
warn "Oauth token present and valid... Using existing token.\n";
return;
}
my %form = (
'grant_type' => 'client_credentials',
'scope' => 'NSMS',
'client_id' => $consumer_key,
'client_secret' => $consumer_secret,
);
my $res = $ua->post( 'https://sapi.telstra.com/v1/oauth/token', \%form );
if ( $res->status_line =~ m/200/ ) {
warn "Successfully obtained new OAuth token...\n";
my $response = JSON->new->decode($res->decoded_content);
$hash->{token} = $response->{access_token};
$hash->{token_expires} = $response->{expires_in} + time() - 60;
## Write the file to disk.
store $hash, dirname($0) . "/tokenstore.bin" or warn "Unable to write token store to disk. Proceeding anyway. $!";
}
return;
}
return 1;