Teams tab: Get token to call Graph (POST request)

Brass Contributor

I have a Teams tab with html/js code. I need to get an access token to call Graph API

When I send my POST request in a browser page (https://reqbin.com/) I get the response:
   {
      "token_type": "Bearer",
      "expires_in": 3599,
      "ext_expires_in": 3599,
      "access_token": "eyJ0eXA....LO40xw"
   }


When I send a POST in my bot node js code I get the response too:


var options = {
uri: 'https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token',
body: 'client_id=xxxx&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=yyyy&grant_type=client_credentials',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST',
};

var request = require('request');
request(options, (error, response, resBody) => { console.log(JSON.stringify(body)); });

log:
"{\"token_type\":\"Bearer\",\"expires_in\":3599,\"ext_expires_in\":3599,\"access_token\":\"eyJ0e...A\"}"

 

But I need to get the token from my html code (tab page). I have this code but failed:

 

var url = 'https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token';
var sBody = 'client_id=xxxx&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=yyyy&grant_type=client_credentials';

fetch(url, {
   method: 'POST', // *GET, POST, PUT, DELETE, etc.
   mode: 'no-cors', // no-cors, *cors, same-origin
   cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
   credentials: 'omit', // include, *same-origin, omit
   headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
   redirect: 'follow', // manual, *follow, error
   referrerPolicy: 'no-referrer', // no-referrer, *client
   body: sBody // body data type must match "Content-Type" header
   }).then(res => console.log(res))
   .then(data => console.log(data));

   

I get an 'opaque' response, I read about 'cors' header: "With an opaque response we won't be able to read the data returned..."

 

But i don't know hoy to fix it. What I tried failed...

 

What am I doing wrong?

 

Thanks in advance,

Diego

2 Replies

@diegoSpace Are you trying to do Tab authentication? Could you please try this sample out and see if you are able to get it working?

Yes, I am trying to do Tab authentication. I will try simple sample, thanks!