python request microsoft graph_如何在没有用户界面交互的情况下使用python连接microsoftgraph?...

在python ADAL sdk中,使用_username_password方法的acquire_token_不接受客户端机密,因此您需要注册native应用程序。在

示例代码:import adal

import requests

tenant = ""

client_id = "app id"

# client_secret = ""

username = ""

password = "”

authority = "https://login.microsoftonline.com/" + tenant

RESOURCE = "https://graph.microsoft.com"

context = adal.AuthenticationContext(authority)

# Use this for Client Credentials

#token = context.acquire_token_with_client_credentials(

# RESOURCE,

# client_id,

# client_secret

# )

# Use this for Resource Owner Password Credentials (ROPC)

token = context.acquire_token_with_username_password(RESOURCE, username, password, client_id)

graph_api_endpoint = 'https://graph.microsoft.com/v1.0{0}'

# /me only works with ROPC, for Client Credentials you'll need /

request_url = graph_api_endpoint.format('/me')

headers = {

'User-Agent' : 'python_test',

'Authorization' : 'Bearer {0}'.format(token["accessToken"]),

'Accept' : 'application/json',

'Content-Type' : 'application/json'

}

response = requests.get(url = request_url, headers = headers)

print (response.content)

此外,如果使用restapi,则不需要原生应用程序,但需要设置client secret。在

PSDFc.png

希望对你有帮助。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值