Azure active directory在授权api调用和获取cod时跳过登录提示

2024-04-26 07:47:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用例,我需要阅读来自outlook的邮件,根据文档,我需要进行2个api调用,如下所示

1https://login.microsoftonline.com/common/oauth2/v2.0/authorize

2https://login.microsoftonline.com/common/oauth2/v2.0/token

在第一个api调用中,我已经传递了所需的参数,如client\ id、redirect\ uri、response\ type和scope

我可以在web浏览器的第一个api中获取访问代码,而在postman中获取html代码

有没有任何方法可以通过编程方式获取访问代码?。你知道吗


Tags: 代码文档comclienttokenapi参数邮件
1条回答
网友
1楼 · 发布于 2024-04-26 07:47:00

听起来你想Get access without a user,请参考^{}一节。你知道吗

下面是我的Python和节点.js,及其结果,如下所示。你知道吗

使用^{}包的Python代码

import requests

tenant = "<your tenant id>"
url = f"https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token"

client_id = "<your client id>"
client_secret = "<your client secret>"

body = {'client_id': client_id, 'scope': 'https://graph.microsoft.com/.default', 'client_secret': client_secret, 'grant_type': 'client_credentials'}

r = requests.post(url, data=body)

json_text = r.text

import json
j = json.loads(json_text)
print('The json object of /token response =>', j)
access_token = j['access_token']
print('access_token =>', access_token)

enter image description here

你知道吗节点.js使用^{}包的代码

const request = require('request');
const querystring = require('querystring');

const tenant = '<your tenant id>';
var url = `https://login.microsoftonline.com/${tenant}/oauth2/v2.0/token`;

var client_id = "<your client id>";
var client_secret = "<your client secret>";

var body = querystring.stringify({'client_id': client_id, 'scope': 'https://graph.microsoft.com/.default', 'client_secret': client_secret, 'grant_type': 'client_credentials'});

request.post({
    headers: {'Content-Type' : 'application/x-www-form-urlencoded'},
    url:     url,
    body:    body
  }, function(error, response, body){
    console.log('The json object of /token response =>', body);
    var j = JSON.parse(body);
    console.log('access_token =>', j['access_token']);
  });

enter image description here

相关问题 更多 >