web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Graph API request to O...
Power Automate
Answered

Graph API request to Outlook from PAD?

(0) ShareShare
ReportReport
Posted on by 51

Dipping my toe in to using Graph API - I can get the API to work fine from the Graph Explorer website but in PAD I'm getting a 401 error. Just using HTTP authentication on this with my 365 account (which is what I'm using on the Graph website without issue.)

 

Capture1.PNG

The responses to this are:

 

StatusCode: 401

WebServiceResponse: {"error":{"code":"InvalidAuthenticationToken","message":"CompactToken parsing failed with error code: 80049217","innerError":{"date":"2023-01-16T16:54:17","request-id":"bc231c66-6b89-4e8b-88f9-bafa0435705b","client-request-id":"bc231c66-6b89-4e8b-88f9-bafa0435705b"}}}

 

Must say I'm not really sure where this authentication token is meant to be used? Does this require the application registration in Azure AD? I've seen a couple of videos where this isn't done so I'm a bit confused. 

I have the same question (0)
  • Kaif_Siddique Profile Picture
    2,108 Super User 2024 Season 1 on at

    Any update on this i am facing the same issue..

  • VJR Profile Picture
    7,635 on at

    @kylebunkin 

     

    Since the link from where you've taken the example says using a Bearer token,...

     

    VJR_0-1673924809573.png

     

     

    ...in the Custom Headers box give it as below.

     

    Authorization: Bearer <YourTokenHere>

     

     

    I haven't used the API but I believe you will get this token from the Azure portal.

     

  • kylebunkin Profile Picture
    51 on at

    Okay I've changed the invoke web service option to include a bearer token (generated from Azure) and the URL to be the endpoint of the Azure application.

    I'm now getting a Status Code 200 response (which I assume is good) but I'm getting the following:

     

    WebServiceResponse:

    !function(){var e=window,o=e.document,i=e.$Config||{};if(e.self===e.top){o&&o.body&&(o.body.style.display="block")}else if(!i.allowFrame){var s=e.self.location.href,l=s.indexOf("#"),n=-1!==l,t=s.indexOf("?"),f=n?l:s.length,d=-1===t||n&&t>l?"?":"&";s=s.substr(0,f)+d+"iframe-request-id="+i.sessionId+s.substr(f),e.top.location=s}}();

    //]]></script>
    <script type="text/javascript">
    //<![CDATA[
    (function () {
    var $Prefetch={"rfPre":true,"delay":5000,"maxHistory":4,"maxAge":43200,"ageRes":1440,"name":"clrc","fetch":[{"path":"https://aadcdn.msauth.net/ests/2.1/content/cdnbundles/converged.v2.login.min_8owwt4u-33ps0wawi7tmow2.css","hash":"VTMWozMv","co":true},{"path":"https://aadcdn.msauth.net/ests/2.1/content/cdnbundles/ux.converged.error.strings-en.min_gs1xzsflsu0ie_xpap2_cw2.js","hash":"7yLrg+tQ","co":true}],"mode":5};
    !function(e,t,n){function r(e){x.appendLog&&x.appendLog("Client Prefetch: "+e)}function i(){try{for(var e=t.cookie.split(";"),r=0;r<e.length;r++){var i=e[r];if(i){var o=i.indexOf("=");if(-1!==o){if(i.substr(0,o).trim()===n.name){var u=i.substr(o+1);return JSON.parse(c(u))}}}}}catch(e){}return{}}function o(e,t,n){return e.replace(t,n)}function c(e){return e=o(e,/%5c/g,"\\"),e=o(e,/%3e/g,">"),e=o(e,/%3d/g,"="),e=o(e,/%3c/g,"<"),e=o(e,/%3b/g,";"),e=o(e,/%3a/g,":"),e=o(e,/%2c/g,","),e=o(e,/%27/g,"'"),
    e=o(e,/%22/g,'"'),e=o(e,/%20/g," "),e=o(e,/%25/g,"%")}function u(e){return e=o(e,/%/g,"%25"),e=o(e,/ /g,"%20"),e=o(e,/"/g,"%22"),e=o(e,/'/g,"%27"),e=o(e,/,/g,"%2c"),e=o(e,/:/g,"%3a"),e=o(e,/;/g,"%3b"),e=o(e,/</g,"%3c"),e=o(e,/=/g,"%3d"),e=o(e,/>/g,"%3e"),e=o(e,/\\/g,"%5c")}function f(e){var r=new Date;r.setTime(r.getTime()+C*H),t.cookie=n.name+"="+u(JSON.stringify(e))+";expires="+r.toUTCString()+";path=/; Secure; SameSite=None"}function a(e,t){if(e){if(e.indexOf){return e.indexOf(t)}
    for(var n=0;n<e.length;n++){if(e[n]===t){return n}}}return-1}function h(e,t){return-1!==a(e,t)}function g(e,t){var n=a(e,t);return-1!==n&&(e.splice(n,1),!0)}function l(e,t){for(var n in e){if(e.hasOwnProperty(n)&&!t(n,e[n])){break}}}function s(){var e=(new Date).getTime(),t=D*H;return P.getTime()>e?Math.round(e/t):Math.round((e-P.getTime())/t)}function d(e,t){var n=!1;if(t&&t.length>0){n=!0;for(var r=0;r<t.length;r++){delete e[t[r]]}}return n}function p(e){var t=s()-C,n=t+2*C,r=null,i=0,o=[]
    ;return l(e,function(c){return c<t||c>n?o.push(c):(0===e[c].length?o.push(c):(null===r||c<r)&&(r=c),i++),!0}),null!==r&&i>k&&o.push(r),d(e,o)}function v(e,t,n){r("Fetched: "+e+" Hash: "+t+" isRefresh: "+n);var o=i(),c=s(),u=!1,a=!1;if(l(o,function(e,n){return!h(n,t)||(e!==c?g(o[e],t)&&(a=!0):u=!0,!1)}),!u){var d=o[c]||[];d.push(t),o[c]=d,a=!0}a|=p(o),a&&f(o),b()}function m(t,n,i,o){var c={"method":"GET"};i&&(c.mode="cors"),e.fetch(t,c).then(function(e){
    200===e.status?v(t,n,o):(r("Unexpected response - "+e.status),b())}).then(null,function(e){r("Failed - "+e),b()})}function T(){if(e.XMLHttpRequest&&!J){return new XMLHttpRequest}if(e.ActiveXObject){try{return new ActiveXObject("Msxml2.XMLHTTP")}catch(e){}try{return new ActiveXObject("Microsoft.XMLHttp")}catch(e){}}return null}function w(e,t,n,i,o){r("Fetching - "+t),e.onload=function(){v(t,n,o)},e.onerror=function(){r("XHR failed!"),b()},e.ontimeout=function(){r("XHR timed out!"),b()};try{e.open("GET",t),
    e.withCredentials=i&&!N,e.send()}catch(r){N?setTimeout(function(){v(t,n,o)},0):(N=!0,w(e,t,n,!1,o))}}function O(t,n,i,o){if(e.fetch){return void m(t,n,i,o)}var c=T();if(null!==c){return void w(c,t,n,i,o)}r("Unable to identify a transport option!")}function b(){var e=n.fetch;if(X<e.length&&e[X]){var t=e[X];O(t.path,t.hash,t.co||!1,t.rf||!1),X++}}function y(e){if(e){try{var n=t.createElement("link");n.rel="prefetch",n.href=e,t.head.appendChild(n)}catch(e){}}}function M(){r("Starting"),b(),b()}function S(){
    for(var t=e.$Config||e.ServerData||{},r=n.fetch,i=n.mode||-1,o=-1,c=0;c<r.length;c++){0!==o&&i>=3&&(o=r[c].rf?1:0),R&&!J&&y(r[c].path||{})}t.prefetchPltMode=o}if(n&&n.fetch&&0!==n.fetch.length){var x=e.$Debug||{},X=0,H=6e4,P=new Date(2019),k=n.maxHistory||4,C=n.maxAge||20160,D=n.ageRes||1440,L=n.delay||5e3,R=n.rfPre||!1,J=void 0!==(e._phantom||e.callPhantom),N=!1;JSON&&JSON.parse&&(n.clearCookie=function(){document.cookie=n.name+"=; expires=Thu, 01 Jan 1970 00:00:01 GMT;"},e.$Do.when("doc.load",function(){
    setTimeout(M,L),setTimeout(S,0)}))}}(window,document,$Prefetch);
     

    ;
    })();
    //]]>
    </script>

    WebServiceResponseHeaders:

    Capture2.PNG

    Capture1.PNG

  • VJR Profile Picture
    7,635 on at

    Hi @kylebunkin

     

    Use the same url as earlier.... That is the API url given in the link for send mail. 

    Not sure what made you to change the url unless you found something that told you to change.

     

  • kylebunkin Profile Picture
    51 on at

    Confusion mainly...

    If I use the https://graph.microsoft/v1.0/me/sendMail url and the either the bearer token from Azure or Graph Explorer Access token I get a 401 status code. So I'm confused as to which token it's meant to be (I'm assuming the Azure token)

     

    {"error":{"code":"InvalidAuthenticationToken","message":"CompactToken parsing failed with error code: 80049217","innerError":{"date":"2023-01-17T13:46:06","request-id":"adf64a9f-5e10-4118-b5b1-357f31258644","client-request-id":"adf64a9f-5e10-4118-b5b1-357f31258644"}}}

     

    A bit more info:

    capture3.PNG

  • Verified answer
    kylebunkin Profile Picture
    51 on at

    Have found a solution - it's not using invoke web service, but rather Powershell.

    $clientID = "Client ID goes here"
    $Clientsecret = "Client Secret goes here"
    $tenantID = "Tenant ID goes here"

    $MailSender = "sender@email.com"

    #Connect to GRAPH API
    $tokenBody = @{
        Grant_Type    = "client_credentials"
        Scope         = "https://graph.microsoft.com/.default"
        Client_Id     = $clientId
        Client_Secret = $clientSecret
    }
    $tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody
    $headers = @{
        "Authorization" = "Bearer $($tokenResponse.access_token)"
        "Content-type"  = "application/json"
    }

    #Send Mail    
    $URLsend = "https://graph.microsoft.com/v1.0/users/$MailSender/sendMail"
    $BodyJsonsend = @"
                        {
                            "message": {
                              "subject": "Hello World from Microsoft Graph API",
                              "body": {
                                "contentType": "HTML",
                                "content": "This Mail is sent via Microsoft<br>
                                GRAPH <br>
                                API<br>
                                
                                "
                              },
                              "toRecipients": [
                                {
                                  "emailAddress": {
                                    "address": "recipient1@company.com"
                                  }                      
                                },
                                {
                                  "emailAddress": {
                                    "address": "recipient2@company.com"
                                  }                      
                                }                            
                              ]
                            },
                            "saveToSentItems": "true"
                          }
    "@

    Invoke-RestMethod -Method POST -Uri $URLsend -Headers $headers -Body $BodyJsonsend

     

    Might not be the cleanest way to do it, but by God it works. 

  • VJR Profile Picture
    7,635 on at

     

    Okay @kylebunkin , glad you got it working out using another option.

     

    But just something noticeable.

     

    The URL used in the powershell is 

    https://graph.microsoft.com/v1.0/users/$MailSender/sendMail

     

    whereas the one in the PAD API call is 

    https://graph.microsoft.com/v1.0/me/sendMail

     

    I hope that was not the issue?..maybe worth a check if you still have the old action with you.

     

  • kylebunkin Profile Picture
    51 on at

    Hi @VJR,

     

    That's a bit of Powershell referenced in that URL; users refers to permissions of the API, $MailSender is defined at the top of the script - this is the account sending the mail.

     

    Was discussing it with some other users, and by all accounts to do this through RPA we would need to invoke a web service that authenticates to get the token, parse the message, then invoke another web service to reply with the client secret... to be honest for my purpose the Powershell option works best as it's purely to send an email to us if something in an unattended flow fails, however if the Powershell script fails for whatever reason, I can't really see a way to let me know... maybe more Powershell?! In any case, it works!

  • kylebunkin Profile Picture
    51 on at

    I should say as well, I can't take credit for that script - it came from Michael Seidl at au2mator. Home - au2mator - Self Service Portal

  • Amith444 Profile Picture
    12 on at

    Hi, I have tried this, but I am not able to send mail. I am not getting any errors though. I have copied your script and placed it inside the invoke powershell activity and replaced with the parameters I have withing the script.

    I have tried this in the powershell directly and there is no error at all. I verified all variables output and it is correct. But when the function ( Invoke-RestMethod -Method POST -Uri $URLsend -Headers $headers -Body $BodyJsonsend) is executed I am not getting the email.

     

    Can you help me out by sharing how exactly you made it working? I am looking to send mail without a dependency of outlook installed in the machine, hence relying on graph api. App registrations and all the necessary values like tenant, app and secret is available for me from the azure portal.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Haque Profile Picture

Haque 594

#2
Valantis Profile Picture

Valantis 328

#3
David_MA Profile Picture

David_MA 281 Super User 2026 Season 1

Last 30 days Overall leaderboard