python 3 登录azureAD并调用azure graph api
2017-08-27 21:22
761 查看
parameters.json :
{
"resource": "https://graph.microsoft.com/",
"tenant" : "{your_directoryId}",
"authorityHostUrl" : "https://login.microsoftonline.com",
"clientid" : "{your_app_id}",
"username" : "{username}",
"password" : "{password}"
}
test.py:
import json
import logging
import os
import sys
import adal
parameters_file = (sys.argv[1] if len(sys.argv) == 2 else
os.environ.get('ADAL_SAMPLE_PARAMETERS_FILE'))
if parameters_file:
with open(parameters_file, 'r') as f:
parameters = f.read()
sample_parameters = json.loads(parameters)
else:
raise ValueError('Please provide parameter file with account information.')
authority_url = (sample_parameters['authorityHostUrl'] + '/' +
sample_parameters['tenant'])
GRAPH_RESOURCE = '00000002-0000-0000-c000-000000000000'
RESOURCE = sample_parameters.get('resource', GRAPH_RESOURCE)
#uncomment for verbose log
#turn_on_logging()
context = adal.AuthenticationContext(
authority_url, validate_authority=sample_parameters['tenant'] != 'adfs',
api_version=None)
########################################################
# below is the token based on username/password
########################################################
tokenRet = context.acquire_token_with_username_password(
RESOURCE,
sample_parameters['username'],
sample_parameters['password'],
sample_parameters['clientid'])
token = tokenRet['accessToken']
print('Here is the access token get by username/password')
print(json.dumps(token, indent=2))
########################################################
# below code to get access token based on refresh token
#########################################################
refresh_token = tokenRet['refreshToken']
retToken = context.acquire_token_with_refresh_token(
refresh_token,
sample_parameters['clientid'],
RESOURCE)
token = retToken['accessToken']
print('Here is the token acquired from the refreshing token')
print(json.dumps(token, indent=2))
###########################################################
# below is the user profile information get from azure graph api
# for more information:
# https://developer.microsoft.com/en-us/graph/graph-explorer ############################################################
import urllib2
req = urllib2.Request('https://graph.microsoft.com/v1.0/users')
req.add_header('Authorization', 'Bearer '+token)
resp = urllib2.urlopen(req)
content = resp.read()
print('below is the user profile info:')
print(content)
{
"resource": "https://graph.microsoft.com/",
"tenant" : "{your_directoryId}",
"authorityHostUrl" : "https://login.microsoftonline.com",
"clientid" : "{your_app_id}",
"username" : "{username}",
"password" : "{password}"
}
test.py:
import json
import logging
import os
import sys
import adal
parameters_file = (sys.argv[1] if len(sys.argv) == 2 else
os.environ.get('ADAL_SAMPLE_PARAMETERS_FILE'))
if parameters_file:
with open(parameters_file, 'r') as f:
parameters = f.read()
sample_parameters = json.loads(parameters)
else:
raise ValueError('Please provide parameter file with account information.')
authority_url = (sample_parameters['authorityHostUrl'] + '/' +
sample_parameters['tenant'])
GRAPH_RESOURCE = '00000002-0000-0000-c000-000000000000'
RESOURCE = sample_parameters.get('resource', GRAPH_RESOURCE)
#uncomment for verbose log
#turn_on_logging()
context = adal.AuthenticationContext(
authority_url, validate_authority=sample_parameters['tenant'] != 'adfs',
api_version=None)
########################################################
# below is the token based on username/password
########################################################
tokenRet = context.acquire_token_with_username_password(
RESOURCE,
sample_parameters['username'],
sample_parameters['password'],
sample_parameters['clientid'])
token = tokenRet['accessToken']
print('Here is the access token get by username/password')
print(json.dumps(token, indent=2))
########################################################
# below code to get access token based on refresh token
#########################################################
refresh_token = tokenRet['refreshToken']
retToken = context.acquire_token_with_refresh_token(
refresh_token,
sample_parameters['clientid'],
RESOURCE)
token = retToken['accessToken']
print('Here is the token acquired from the refreshing token')
print(json.dumps(token, indent=2))
###########################################################
# below is the user profile information get from azure graph api
# for more information:
# https://developer.microsoft.com/en-us/graph/graph-explorer ############################################################
import urllib2
req = urllib2.Request('https://graph.microsoft.com/v1.0/users')
req.add_header('Authorization', 'Bearer '+token)
resp = urllib2.urlopen(req)
content = resp.read()
print('below is the user profile info:')
print(content)
相关文章推荐
- asp.net Webapi登录azureAD并调用azure graph api
- 利用python3.4爬取学校教务系统,并封装成api供app调用(二)之不带验证码模拟登录
- [Windows Azure] Using the Graph API to Query Windows Azure AD
- python模拟登录新浪微博自动获得调用新浪api所需的code
- 使用python调用azure api步骤文档
- python模拟登录新浪微博自动获得调用新浪api所需的code
- python下以api形式调用tesseract识别图片验证码
- python django成功调用sina微博api(最新)有图
- 在独立的python文件调用django api
- Azure Mobile Services的REST API调用方式和自定义API
- jenkins的api调用python篇(三)秀代码
- Azure REST API (4) 在Python环境下,使用Azure REST API
- C# 调用API,实现注销远程登录本机的用户 以及 远程登录用户获得自己用户名(转+原);
- Azure AD Connect 用户登录选项介绍
- 【总结】如何通过python调用新浪微博的API
- jenkins的api调用python篇(二)
- saltstack python api 调用
- python调用openstack的api,create_instance的程序解析
- eoLinker-API_Shop_短信服务接口-调用示例代码,支持PHP、Python、Java等语言
- Python调用系统底层API播放wav文件的方法