Oph*_*hir 2 python office365 microsoft-graph-api
尝试编写一个 Python 代码,我想在其中访问我的日历并检索我的日程安排。无法通过身份验证阶段。看过并测试了许多示例,但所有示例都需要运行本地服务器,我在本地浏览并需要单击按钮,然后输入我的凭据。旨在在我的 Python 代码中执行所有这些操作。
您可以通过以下两种方式之一实现此目的:
这是展示这两个内容的代码片段:
import adal
import requests
tenant = "contoso.com"
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
username = "foo@contoso.com"
password = "mypassword"
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 /<UsersObjectId/
request_url = graph_api_endpoint.format('/me')
headers = {
'User-Agent' : 'python_tutorial/1.0',
'Authorization' : 'Bearer {0}'.format(token["accessToken"]),
'Accept' : 'application/json',
'Content-Type' : 'application/json'
}
response = requests.get(url = request_url, headers = headers)
print (response.content)
Run Code Online (Sandbox Code Playgroud)