Skip to content

Commit 5e273b0

Browse files
committed
feat(grant): add support of Client Credentials Grant
1 parent 960bd11 commit 5e273b0

File tree

7 files changed

+45
-20
lines changed

7 files changed

+45
-20
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
fail-fast: false
1717
matrix:
1818
os: [ubuntu-latest, macos-latest]
19-
perl: [ '5.28', '5.30' ]
19+
perl: [ '5.28', '5.30', '5.34' ]
2020

2121
name: Perl ${{ matrix.python }}
2222
steps:

Build.PL

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ my $builder = Module::Build->new(
2525
module_name => 'Net::Upwork::API',
2626
license => 'apache',
2727
dist_author => 'Maksym Novozhylov <[email protected]>',
28-
dist_version => '2.1.4',
28+
dist_version => '2.2.0',
2929
dist_abstract => 'Perl bindings for Upwork API (OAuth2)',
3030
build_requires => {
3131
'Test::More' => '0.66',
@@ -34,7 +34,7 @@ my $builder = Module::Build->new(
3434
requires => {
3535
'perl' => '5.8.8',
3636
'IO::Socket::SSL' => '1.965',
37-
'Net::OAuth2' => '0.63',
37+
'Net::OAuth2' => '0.67',
3838
},
3939
meta_merge => { keywords => [qw( upwork oauth2 )], },
4040
meta_add => {

CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.2.0
2+
* Add support of Client Credentials Grant
3+
14
## 2.1.4
25
* Add GraphQL support
36

example/example.pl

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222
'client_id' => 'xxxxxxxx',
2323
'client_secret' => 'xxxxxxxx',
2424
'redirect_uri' => 'https://your-call-back-url.here',
25+
# 'grant_type' => 'client_credentials', # used for Client Credentials Grant
2526
# 'access_token' => 'xxxxxxxx',
26-
# 'refresh_token' => 'xxxxxxxx',
27+
# 'refresh_token' => 'xxxxxxxx', # used by Code Authorization Grant
2728
# 'expires_in' => 86399 # TTL. `expires_at` should be enough for basic usage but you may find this option useful for own needs
2829
# 'expires_at' => 1234567890 # timestamp, either get from the Net::OAuth2::AccessToken object or set like time()+actual_expires_in
2930
);
3031

3132
$api = Net::Upwork::API->new($config);
3233
if (!$api->has_access_token()) {
34+
# start Code Authorization Grant
3335
my $authz_url = $api->get_authorization_url();
3436

3537
print "Visit the authorization url and provide oauth_verifier for further authorization\n";
@@ -38,6 +40,8 @@
3840
$code = <STDIN>;
3941

4042
my $session = $api->get_access_token($code);
43+
# end Code Authorization Grant
44+
#my $session = $api->get_access_token(); # Client Credentials Grant
4145
#print Dumper $session; # Net::OAuth2::AccessToken object
4246
#print Dumper $session->access_token;
4347
#print Dumper $session->refresh_token;

lib/Net/Upwork/API.pm

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use warnings;
1919
use Net::Upwork::API::Config;
2020
use Net::Upwork::API::Client;
2121

22-
our $VERSION = '2.1.4';
22+
our $VERSION = '2.2.0';
2323

2424
use constant TOKEN_TYPE_BEARER => 'Bearer';
2525

@@ -116,7 +116,9 @@ sub get_access_token {
116116
my $self = shift;
117117
my ($code) = @_;
118118

119-
chomp($code);
119+
if (defined $code) {
120+
chomp($code);
121+
}
120122

121123
$self->{client}{access_token_session} = $self->{client}{oauth_client}->get_access_token($code);
122124

lib/Net/Upwork/API/Client.pm

+29-14
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,35 @@ sub get_oauth_client {
8484
$lwp = LWP::UserAgent->new();
8585
$lwp->agent(UPWORK_LIBRARY_USER_AGENT);
8686

87-
$self->{oauth_client} = Net::OAuth2::Profile::WebServer->new(
88-
client_id => $self->{config}{client_id},
89-
client_secret => $self->{config}{client_secret},
90-
access_token => $self->{config}{access_token},
91-
refresh_token => $self->{config}{refresh_token},
92-
expires_in => $self->{config}{expires_in},
93-
expires_at => $self->{config}{expires_at},
94-
site => BASE_HOST,
95-
authorize_path => URI_AUTH,
96-
access_token_path => URI_ATOKEN,
97-
refresh_token_path => URI_ATOKEN,
98-
redirect_uri => $self->{config}{redirect_uri},
99-
user_agent => $lwp
100-
);
87+
if ($self->{config}{grant_type} eq "client_credentials") {
88+
$self->{oauth_client} = Net::OAuth2::Profile::WebServer->new(
89+
client_id => $self->{config}{client_id},
90+
client_secret => $self->{config}{client_secret},
91+
grant_type => $self->{config}{grant_type},
92+
access_token => $self->{config}{access_token},
93+
expires_in => $self->{config}{expires_in},
94+
expires_at => $self->{config}{expires_at},
95+
site => BASE_HOST,
96+
access_token_path => URI_ATOKEN,
97+
redirect_uri => $self->{config}{redirect_uri},
98+
user_agent => $lwp
99+
);
100+
} else {
101+
$self->{oauth_client} = Net::OAuth2::Profile::WebServer->new(
102+
client_id => $self->{config}{client_id},
103+
client_secret => $self->{config}{client_secret},
104+
access_token => $self->{config}{access_token},
105+
refresh_token => $self->{config}{refresh_token},
106+
expires_in => $self->{config}{expires_in},
107+
expires_at => $self->{config}{expires_at},
108+
site => BASE_HOST,
109+
authorize_path => URI_AUTH,
110+
access_token_path => URI_ATOKEN,
111+
refresh_token_path => URI_ATOKEN,
112+
redirect_uri => $self->{config}{redirect_uri},
113+
user_agent => $lwp
114+
);
115+
}
101116
}
102117

103118
=item get

lib/Net/Upwork/API/Config.pm

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ sub new {
4343
my %opts = @_;
4444
$opts{client_id} ||= "";
4545
$opts{client_secret} ||= "";
46+
$opts{grant_type} ||= "code_authorization";
4647
$opts{access_token} ||= "";
4748
$opts{refresh_token} ||= "";
4849
$opts{expires_in} ||= "";

0 commit comments

Comments
 (0)