如何解决在用户邮件中获取广告
我创建了一个 html 文件并将其托管在 azure 应用服务(kudos-wwwroot 文件夹)中,并通过启用应用注册来启用广告身份验证
有没有办法在html文件中捕获登录用户邮件ID的广告?
html页面使用的扩展名是js和css。
解决方法
感谢您的回复,我认为这可能对您有所帮助。如果您在此 tutorial 之后启用身份验证,我认为您可以调用 /.auth/me
来获取 userName 声明,这是针对这种情况的另一个 answer,我也尝试过,它必须有所帮助。
如果使用msal.js来实现认证,通过调用图api(https://graph.microsoft.com/v1.0/me)也很容易收集用户信息,这里有一个例子。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Document</title>
<script src="js/msal.js"></script>
</head>
<body>
<div style="margin-top: 15px; background-color: #DDDDDD;">
<button type="button" id="signIn" onclick="signIn()">Sign In</button>
<button type="button" id="getAccessToken" onclick="getAzureAccessToken()">getAccessToken</button>
<button type="button" id="accessApi" onclick="accessApi()">getApiResponse</button>
<h5 class="card-title" id="welcomeMessage">Please sign-in to see your profile and read your mails</h5>
<div>
<div>
accesstoken :
<div id="accesstoken">
</div>
</div>
<div id="">
api response :
<div id="json">
</div>
</div>
</div>
</div>
<script type="text/javascript">
const msalConfig = {
auth: {
clientId: "application_id",authority: "https://login.microsoftonline.com/your_tenant",redirectUri: "http://localhost:8848/implicitGrantFlowAuthAndCallApi/new_file.html",},cache: {
cacheLocation: "sessionStorage",// This configures where your cache will be stored
storeAuthStateInCookie: false,// Set this to "true" if you are having issues on IE11 or Edge
}
};
const loginRequest = {
scopes: ["openid","profile","User.Read"]
};
//scope for generate access token
const AzureMgmtScops ={
scopes:["User.Read"]
}
//api endpoint
const apiConf = {
endpoint:"https://graph.microsoft.com/v1.0/me"
};
let accessToken = '';
const myMSALObj = new Msal.UserAgentApplication(msalConfig);
function signIn() {
myMSALObj.loginPopup(loginRequest)
.then(loginResponse => {
console.log("id_token acquired at: " + new Date().toString());
console.log(loginResponse);
if (myMSALObj.getAccount()) {
showWelcomeMessage(myMSALObj.getAccount());
getAzureAccessToken();
}
}).catch(error => {
console.log(error);
});
}
function showWelcomeMessage(account) {
document.getElementById("welcomeMessage").innerHTML = `Welcome ${account.name}`;
}
function getAzureAccessToken(){
myMSALObj.acquireTokenSilent(AzureMgmtScops).then(tokenResponse => {
showAccesstoken(tokenResponse.accessToken)
accessToken = tokenResponse.accessToken;
sessionStorage.setItem("accessToken",accessToken);
callMSGraph(apiConf.endpoint,tokenResponse.accessToken,showResult);
}).catch(function (error) {
console.log(error);
})
}
function accessApi(){
callMSGraph(apiConf.endpoint,accessToken,showResult);
}
function callMSGraph(endpoint,token,callback) {
const headers = new Headers();
const bearer = `Bearer ${token}`;
// headers.append("Content-Type","application/json");
headers.append("Authorization",bearer);
const options = {
method: "GET",headers: headers
};
console.log('request made to Graph API at: ' + new Date().toString());
fetch(endpoint,options)
.then(response => response.json())
.then(response => callback(response,endpoint))
.catch(error => console.log(error))
}
function showAccesstoken(data){
document.getElementById("accesstoken").innerHTML = JSON.stringify(data,null,2);
}
function showResult(data){
document.getElementById("json").innerHTML = JSON.stringify(data,2);
}
</script>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。