PeterKaagman has asked for the wisdom of the Perl Monks concerning the following question:
Hi there,
Not really sure this is an actual Perl question. But knowing your wisdom... I'll give a shot here.
Thing is I need to interact with MS Graph. Actually got it working in NodeJS, but I really don't like NodeJS... running back home... Perl
I've got an App registration in Azure to retrieve groups information. Got the APP_ID, APP_Secret and Tenant_id... these work just fine with Node. For Perl it seems I have a choice of cURL or LWP. Allthough I would prefer LWP I have chosen cURL for the moment. Did some work with it and there is more info on cURL/MS Graph than LWP/MS Graph.
This is the code I came up with:
#! /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 di +e("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&clie +nt_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; } }
This is based on a pure cURL example which I found here
Running this code gets me the following output:
Bareword "Types::Serialiser::Error::" refers to nonexistent package at + /usr/share/perl5/Types/Serialiser.pm line 136. Bareword "Types::Serialiser::Error::" refers to nonexistent package at + /usr/share/perl5/Types/Serialiser.pm line 147. ==LogIn== Return code login: 0 ==Fetch== https://graph.microsoft.com/v1.0/groups eyJ0eXAiOi[edited out most of the token]eRppGoilg Return code fetch: 0 $VAR1 = { 'error' => { 'innerError' => { 'client-request-id' => 'f49ce +921-3ceb-4613-a901-3eb75a9126a2', 'date' => '2023-01-18T10:52:4 +3', 'request-id' => 'f49ce921-3ce +b-4613-a901-3eb75a9126a2' }, 'code' => 'InvalidAuthenticationToken', 'message' => 'Access token validation failure. +Invalid audience.' } };
I am able to get a token, but I cannot make requests with that token. (kinda ignored the serializer warnings)
Googling on the error codes given does not help a whole lot. I get things like wrong App registrations. But I'm pretty sure that part is ok. I suspect there is something wrong with how I put the token in the header. Below the cURL snippet on which I based my code for the groups request:
curl -X GET -H "Authorization: Bearer [TOKEN]" -H "Content-Type: appli +cation/json" https://management.azure.com/subscriptions/[SUBSCRIPTION +_ID]/providers/Microsoft.Web/sites?api-version=2016-08-01
Kinda at a loss. Am aware this smell like a MS Graph issue, but got good hints here in the past. Any help, even a RTFM (with pointer to the manual ;)), wil be greatly appreciated.
BTW
Did indeed try to get it to work with LWP. Was able to get a token but had some difficulty composing a header with the token
Regards
Peter
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problems with MS Graph
by Corion (Patriarch) on Jan 18, 2023 at 12:11 UTC | |
|
Re: Problems with MS Graph
by Discipulus (Canon) on Jan 18, 2023 at 12:07 UTC | |
|
Re: Problems with MS Graph
by bliako (Abbot) on Jan 19, 2023 at 11:34 UTC | |
by PeterKaagman (Beadle) on Jan 20, 2023 at 10:16 UTC | |
by bliako (Abbot) on Jan 20, 2023 at 15:50 UTC | |
by PeterKaagman (Beadle) on Jan 22, 2023 at 22:49 UTC | |
by bliako (Abbot) on Jan 23, 2023 at 11:54 UTC | |
|
Re: Problems with MS Graph
by PeterKaagman (Beadle) on Jan 18, 2023 at 12:21 UTC | |
by ErichStrelow (Initiate) on Feb 16, 2023 at 22:53 UTC |