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

%3CLINGO-SUB%20id%3D%22lingo-sub-1125450%22%20slang%3D%22en-US%22%3ERe%3A%20Teams%20tab%3A%20Get%20token%20to%20call%20Graph%20(POST%20request)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1125450%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F402611%22%20target%3D%22_blank%22%3E%40diegoSpace%3C%2FA%3E%26nbsp%3BAre%20you%20trying%20to%20do%20Tab%20authentication%3F%20Could%20you%20please%20try%20this%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FOfficeDev%2Fmicrosoft-teams-sample-complete-node%2Fblob%2Fmaster%2Fsrc%2Fviews%2Ftab-auth%2Fsimple.hbs%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esample%3C%2FA%3E%20out%20and%20see%20if%20you%20are%20able%20to%20get%20it%20working%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1114714%22%20slang%3D%22en-US%22%3ETeams%20tab%3A%20Get%20token%20to%20call%20Graph%20(POST%20request)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1114714%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20Teams%20tab%20with%20html%2Fjs%20code.%20I%20need%20to%20get%20an%20access%20token%20to%20call%20Graph%20API%3C%2FP%3E%3CP%3EWhen%20I%20send%20my%20POST%20request%20in%20a%20browser%20page%20(%3CA%20href%3D%22https%3A%2F%2Freqbin.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Freqbin.com%2F%3C%2FA%3E)%20I%20get%20the%20response%3A%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%22token_type%22%3A%20%22Bearer%22%2C%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%22expires_in%22%3A%203599%2C%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%22ext_expires_in%22%3A%203599%2C%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%22access_token%22%3A%20%22eyJ0eXA....LO40xw%22%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%7D%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EWhen%20I%20send%20a%20POST%20in%20my%20bot%20node%20js%20code%20I%20get%20the%20response%20too%3A%3C%2FP%3E%3CP%3E%3CBR%20%2F%3Evar%20options%20%3D%20%7B%3CBR%20%2F%3Euri%3A%20'%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%7Btenant%7D%2Foauth2%2Fv2.0%2Ftoken%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%7Btenant%7D%2Foauth2%2Fv2.0%2Ftoken%3C%2FA%3E'%2C%3CBR%20%2F%3Ebody%3A%20'client_id%3Dxxxx%26amp%3Bscope%3Dhttps%253A%252F%252Fgraph.microsoft.com%252F.default%26amp%3Bclient_secret%3Dyyyy%26amp%3Bgrant_type%3Dclient_credentials'%2C%3CBR%20%2F%3Eheaders%3A%20%7B%20'Content-Type'%3A%20'application%2Fx-www-form-urlencoded'%20%7D%2C%3CBR%20%2F%3Emethod%3A%20'POST'%2C%3CBR%20%2F%3E%7D%3B%3CBR%20%2F%3E%3CBR%20%2F%3Evar%20request%20%3D%20require('request')%3B%3CBR%20%2F%3Erequest(options%2C%20(error%2C%20response%2C%20resBody)%20%3D%26gt%3B%20%7B%20console.log(JSON.stringify(body))%3B%20%7D)%3B%3C%2FP%3E%3CP%3Elog%3A%3CBR%20%2F%3E%22%7B%5C%22token_type%5C%22%3A%5C%22Bearer%5C%22%2C%5C%22expires_in%5C%22%3A3599%2C%5C%22ext_expires_in%5C%22%3A3599%2C%5C%22access_token%5C%22%3A%5C%22eyJ0e...A%5C%22%7D%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20I%20need%20to%20get%20the%20token%20from%20my%20html%20code%20(tab%20page).%20I%20have%20this%20code%20but%20failed%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Evar%20url%20%3D%20'%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%7Btenant%7D%2Foauth2%2Fv2.0%2Ftoken%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%7Btenant%7D%2Foauth2%2Fv2.0%2Ftoken%3C%2FA%3E'%3B%3CBR%20%2F%3Evar%20sBody%20%3D%20'client_id%3Dxxxx%26amp%3Bscope%3Dhttps%253A%252F%252Fgraph.microsoft.com%252F.default%26amp%3Bclient_secret%3Dyyyy%26amp%3Bgrant_type%3Dclient_credentials'%3B%3CBR%20%2F%3E%3CBR%20%2F%3Efetch(url%2C%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3Bmethod%3A%20'POST'%2C%20%2F%2F%20*GET%2C%20POST%2C%20PUT%2C%20DELETE%2C%20etc.%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3Bmode%3A%20'no-cors'%2C%20%2F%2F%20no-cors%2C%20*cors%2C%20same-origin%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3Bcache%3A%20'no-cache'%2C%20%2F%2F%20*default%2C%20no-cache%2C%20reload%2C%20force-cache%2C%20only-if-cached%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3Bcredentials%3A%20'omit'%2C%20%2F%2F%20include%2C%20*same-origin%2C%20omit%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3Bheaders%3A%20%7B%20'Content-Type'%3A%20'application%2Fx-www-form-urlencoded'%20%7D%2C%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3Bredirect%3A%20'follow'%2C%20%2F%2F%20manual%2C%20*follow%2C%20error%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3BreferrerPolicy%3A%20'no-referrer'%2C%20%2F%2F%20no-referrer%2C%20*client%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3Bbody%3A%20sBody%20%2F%2F%20body%20data%20type%20must%20match%20%22Content-Type%22%20header%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%7D).then(res%20%3D%26gt%3B%20console.log(res))%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B.then(data%20%3D%26gt%3B%20console.log(data))%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%3CP%3EI%20get%20an%20'opaque'%20response%2C%20I%20read%20about%20'cors'%20header%3A%20%22%3CSPAN%3EWith%20an%20opaque%20response%20we%20won't%20be%20able%20to%20read%20the%20data%20returned...%22%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EBut%20i%20don't%20know%20hoy%20to%20fix%20it.%20What%20I%20tried%20failed...%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20am%20I%20doing%20wrong%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%2C%3C%2FP%3E%3CP%3EDiego%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1114714%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMicrosoft%20Teams%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
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
Highlighted

@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?

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