Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Problems with MS Graph

by bliako (Monsignor)
on Jan 19, 2023 at 11:34 UTC ( [id://11149684]=note: print w/replies, xml ) Need Help??


in reply to Problems with MS Graph

There is LWP::Curl which is the LWP API with the curl engine underneath.

With LWP there are some debug options (untested, from memory):

use LWP::ConsoleLogger::Easy qw( debug_ua ); use LWP::UserAgent; my $ua = LWP::UserAgent->new(...); debug_ua($ua, 10); ...

This should print out the headers of any request. That said, if you have a working curl command-line then curl2lwp is your best bet to convert it to a working LWP-based perl script. It worked for me every time.

btw, it does not look like you need cookie persistence (as per your curl command) but if you do:

use LWP::UserAgent; use HTTP::Cookies; my $cookies = HTTP::Cookies->new(file =>'./cookies.txt'); my $ua = LWP::UserAgent->new(...); $ua->cookie_jar($cookies);

Also, perhaps you want to try resolve the error Bareword "Types::Serialiser::Error::" refers to nonexistent package... by reinstalling Types::Serialiser? I am not sure how to resolve it.

bw, bliako

Replies are listed 'Best First'.
Re^2: Problems with MS Graph
by PeterKaagman (Sexton) on Jan 20, 2023 at 10:16 UTC

    Did not notice you reply before, sorry for that, thanks for you trying to help

    I do not think I need cookie persistance either, but wil give it a go since all my other efforts seem to fail.

    Did try a pure curl approach in a bash script, pasting in the token I was able to get. That failed as wel. Bugs me that Node (using Axios) does not have this problem.

    Not really bothered by the serializer error atm... does not seem to interfere

      So, ***bash/curl*** and Perl fail where Node succeeds. I guess you need to watch the headers that Node sends and see how they compare to the other 2 methods. I have shown you how to check the headers with Perl. You need to do the same with Node. Or it may be easier via the browser if you have successfull access with that (developer tools, monitor the network request)

      Please update your posts to the fact that curl does not work (if I understood correctly). That's an important point.

        No, bash/curl nor Perl fails. The problem was between the keyboard and the chair, as allways.

        After an "Oh shit" experience when I found out I had forgotten to define the scope for the token request it still did not work. After some (actually a lot) googling I found that I allso had to ad a resouce in the request. In the process learned about "jq" and came up with the following bash script:

        #! /usr/bin/bash token=`curl \ -d grant_type=client_credentials \ -d client_id=[client_id] \ -d client_secret=[client_secret] \ -d scope=https://graph.microsoft.com/.default \ -d resource=https://graph.microsoft.com \ https://login.microsoftonline.com/[tenant_id]/oauth2/token \ | jq -j .access_token` curl -X GET \ -H "Authorization: Bearer $token" \ -H "Content-Type: application/json" \ https://graph.microsoft.com/v1.0/groups \ | jq .

        This does the job. Gets a token an uses that token to request a listing of groups.
        Getting that I could use the curl2lwp site and came up with the following Perl script

        #! /usr/bin/perl -W use strict; use Data::Dumper; use JSON; use Config::Simple; use FindBin; #use lib "$FindBin::Bin/../lib"; use LWP::UserAgent; my %config; Config::Simple->import_from("$FindBin::Bin/groups.cfg",\%config) or di +e("No config: $!"); my $ua = LWP::UserAgent->new( 'send_te' => '0', ); sub login_app { # {{{1 my $url = "$config{'LOGIN_ENDPOINT'}/$config{'TENANT_ID'}/oauth2/t +oken"; my $r = HTTP::Request->new( 'POST' => $url, [ 'Accept' => '*/*', 'User-Agent' => 'curl/7.55.1', 'Content-Type' => 'application/x-www-form-urlenc +oded' ], "grant_type=client_credentials&client_id=$config{'APP_ID'} +&client_secret=$config{'APP_PASS'}&scope=$config{'GRAPH_ENDPOINT'}/.d +efault&resource=$config{'GRAPH_ENDPOINT'}" ); my $result = $ua->request($r); if ($result->is_success){ return decode_json($result->decoded_content) }else{ print Dumper $result; die $result->status_line; } }# }}} sub fetch { # {{{1 my $token = shift; my $url = shift; my $r = HTTP::Request->new( 'GET' => $url, [ 'Accept' => '*/*', 'Authorization' => "Bearer $token", 'User-Agent' => 'curl/7.55.1', 'Content-Type' => 'application/json' ], ); my $result = $ua->request($r); if ($result->is_success){ return decode_json($result->decoded_content) }else{ print Dumper $result; die $result->status_line; } }# }}} my $token_request = login_app(); print Dumper $token_request; if ($$token_request{'access_token'}){ my $url = "$config{'GRAPH_ENDPOINT'}/v1.0/groups"; my $groups = fetch($$token_request{'access_token'}, $url); print Dumper $groups; }

        I think the data line in the request is kinda ugly. There is problably a better/neater way of doing that. But I'm satisfied for now

        Thanks all for the help, appreciate it a lot!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11149684]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-18 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found