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


In reply to Problems with MS Graph by PeterKaagman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.