#! /usr/bin/perl -W use strict; use WWW::Curl::Easy; use Data::Dumper; use JSON; use Config::Simple; use FindBin; #use lib "$FindBin::Bin/../lib"; my %config; Config::Simple->import_from("$FindBin::Bin/groups.cfg",\%config) or die("No config: $!"); my $c = WWW::Curl::Easy->new(); sub login_app { # {{{1 my $response; my $response_body; print "==LogIn==\n"; my @Headers = ( "Content-Type: application/x-www-form-urlencoded" ); $c->setopt(CURLOPT_URL, "https://$config{'LOGIN_ENDPOINT'}/$config{'TENANT_ID'}/oauth2/token"); $c->setopt(CURLOPT_HEADER, "0"); $c->setopt(CURLOPT_WRITEDATA, \$response_body); $c->setopt(CURLOPT_SSL_VERIFYHOST, "1"); $c->setopt(CURLOPT_SSL_VERIFYPEER, "1"); $c->setopt(CURLOPT_POSTFIELDS, "grant_type=client_credentials&client_id=$config{'APP_ID'}&client_secret=$config{'APP_PASS'}"); $c->setopt(CURLOPT_HTTPHEADER, \@Headers); $c->setopt(CURLOPT_CUSTOMREQUEST, 'POST'); my $retcode = $c->perform(); print "Return code login: $retcode\n"; if ($retcode == 0){ my $response = decode_json($response_body); #print Dumper $response; return $response; }else{ print("An error occured: $retcode\n".$c->strerror($retcode)." ".$c->errbuf."\n"); return 0; } }# }}} sub fetch { my $token = shift; my $url = shift; my $method = shift; my $response; my $response_body; print "==Fetch==\n"; print "$url\n"; print "$token\n"; my @Headers = ( "Authorization: Bearer $token", 'Content-Type: application/json' ); #print Dumper \@Headers; $c->setopt(CURLOPT_URL, "$url"); $c->setopt(CURLOPT_HEADER, "0"); $c->setopt(CURLOPT_WRITEDATA, \$response_body); $c->setopt(CURLOPT_SSL_VERIFYHOST, "0"); $c->setopt(CURLOPT_SSL_VERIFYPEER, "0"); $c->setopt(CURLOPT_HTTPHEADER, \@Headers); $c->setopt(CURLOPT_CUSTOMREQUEST, $method); my $retcode = $c->perform(); print "Return code fetch: $retcode\n"; if ($retcode == 0){ my $response = decode_json($response_body); return $response; }else{ print("An error occured: $retcode\n".$c->strerror($retcode)." ".$c->errbuf."\n"); return 0; }} my $token_request = login_app(); if ($token_request){ if ($$token_request{'access_token'}){ my $url = "https://$config{'GRAPH_ENDPOINT'}/v1.0/groups"; my $groups = fetch($$token_request{'access_token'},$url,'GET'); print Dumper $groups; }else{ print "Geen token\n"; print Dumper $token_request; } }