我正在编写一个Azure函数,该函数需要从Microsoft获得的OAuth令牌,而我已经能够成功获取它。我正在尝试使用该令牌访问Microsoft Graph。当我从Microsoft收到令牌后,我的函数在十分钟后超时,并且没有过期,context.log('CALLING MS GRAPH'.)我是Azure的新手,无法弄清楚为什么我不能使用的值调用我的第二个函数Microsoft返回的令牌或具有硬编码值的令牌。
任何帮助是极大的赞赏 :)
我尝试将令牌值硬编码到函数中,更改超时时间,并添加各种context.log()-但是无法过去接收令牌。我还尝试过将.end()删除到我的POST调用中。
const https = require('https');
const querystring = require('querystring');
getAccessToken = (context, callback) => {
const postData = querystring.stringify({
'client_id': {clientID},
'scope': 'https://graph.microsoft.com/.default',
'client_secret': {clientSecret},
'grant_type': 'client_credentials'
});
const msTokenOptions = {
hostname: 'login.microsoftonline.com',
port: 443,
path: `/${tenantID}}/oauth2/v2.0/token`,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
const oauthReq = https.request(msTokenOptions, (res) => {
res.setEncoding('utf8');
res.on('data', (d) => {
let accessToken = JSON.parse(d).access_token;
// Error happens here.
context.log('CALLING MSGRAPH')
// I never make it into the functions below, regardless of how they're called.
callback(accessToken);
accessMsGraph(accessToken)
});
});
oauthReq.on('error', (e) => {
context.log('ERROR: Problem obtaining MS Token. ' + e);
});
oauthReq.write(postData);
oauthReq.end();
return;
};
accessMsGraph = (token) => {
// GET request to MS Graph here - I never make it into this function.
};
module.exports = (context, req) => {
getAccessToken(context, (token) => {
context.log('Accessing graph')
accessMsGraph(context, token)
accessMsGraph('123456')
});
};
眼眸繁星
相关分类