# Create a group object to get owners and members
my $group_object = MsGroup->new(
'app_id' => $config{'APP_ID'},
'app_secret' => $config{'APP_PASS'},
'tenant_id' => $config{'TENANT_ID'},
'login_endpoint'=> $config{'LOGIN_ENDPOINT'},
'graph_endpoint'=> $config{'GRAPH_ENDPOINT'},
'select' => '$select=id,displayName,userPrincipalName',
'id' => $group->{'id'},
);
# Get the owners
my $owners = $group_object->fetch_owners();
# $owners is een AOH
foreach my $owner (@$owners){
# do something usefull with the owner
}
# Get the members
my $members = $group_object->fetch_members();
# $members is een AOH
foreach my $member (@$members){
# do something usefull with the members }
}
####
sub fetch_members { # {{{1
my $self = shift; # get a reference to the object itself
my @members; # an array to hold the result
# compose an URL
my $url = $self->_get_graph_endpoint . "/v1.0/groups/".$self->_get_id."/members/?";
# add a filter if needed (not doing any filtering though)
if ($self->_get_filter){
$url .= $self->_get_filter."&";
}
# add a selectif needed, have in fact a select => see object creation
if ($self->_get_select){
$url .= $self->_get_select;
}
$url .= '&$count=true'; # adding $count just to be sure
do_fetch($self,$url, \@members); # actual fetch is done in do_fetch()
return \@members; # return a reference to the resul
}# }}}
####
sub do_fetch { # {{{1
my $self = shift; # get a reference to the object
my $url = shift; # get the URL from the function call
my $found = shift; # get the array reference which holds the result
my $result = $self->callAPI($url, 'GET'); # do_fetch calls callAPI to do the HTTP request
# Process if rc = 200
if ($result->is_success){
my $reply = decode_json($result->decoded_content);
while (my ($i, $el) = each @{$$reply{'value'}}) {
push @{$found}, $el;
}
# do a recursive call if @odata.nextlink is there
if ($$reply{'@odata.nextLink'}){
do_fetch($self,$$reply{'@odata.nextLink'}, $found);
}
#print Dumper $$reply{'value'};
}else{
# Error handling
print Dumper $result;
die $result->status_line;
}
} # }}}
####
sub callAPI { # {{{1
my $self = shift; # Get a refence to the object itself
my $url = shift; # Get the URL from the function call
my $verb = shift; # Get the method form the function call
my $try = shift || 1;
my $ua = LWP::UserAgent->new( # Create a LWP useragnent (beyond my scope, its a CPAN module)
'timeout' => '5',
);
# Create the header
my @header = [
'Accept' => '*/*',
'Authorization' => "Bearer ".$self->_get_access_token,
'User-Agent' => 'curl/7.55.1',
'Content-Type' => 'application/json',
'Consistencylevel' => $self->_get_consistencylevel
];
# Create the request
my $r = HTTP::Request->new(
$verb => $url,
@header,
);
# Let the useragent make the request
my $result = $ua->request($r);
# adding error handling
# rc 429 is throttling
if (! $result->{"_rc"} eq "200"){
print Dumper $result;
}
return $result;
} # }}}