【发布时间】:2017-02-28 09:37:12
【问题描述】:
我使用 UCWA 和密码令牌开发了一个应用程序。我正在阅读使用事件通过应用程序进行身份验证的用户收到的所有消息,但令牌不会持续很长时间,并且更新使用的是浏览器,这对于自动化来说很糟糕。
有没有办法获得一个不需要通过浏览器更新的令牌,这样我就可以让我的应用程序完全自动化?我已经阅读了 Github 和 ucwa 网站上的所有文档。
这是我获取令牌的请求。
获取登录网址
def get_signin_url(redirect_uri,client_id,租户,资源): xframe, user_discovery_uri, resource = do_autodiscover(config['domain'])
# Build the query parameters for the signin url
params = {
'client_id': client_id,
'redirect_uri': redirect_uri,
'response_type': 'token',
'response_mode': 'form_post',
'resource': resource
}
# The authorize URL that initiates the OAuth2 client credential flow for admin consent
authorize_url = '{0}{1}'.format(authority, '/%s/oauth2/authorize?{0}' % tenant)
# Format the sign-in url for redirection
signin_url = authorize_url.format(urlencode(params))
return signin_url
经过几个步骤,得到token:
def get_token_from_code(client_id, tenant, auth_code, redirect_uri, resource, client_secret):
# Build the post form for the token request
post_data = {
'grant_type': 'authorization_code',
'code': auth_code,
'redirect_uri': redirect_uri,
'resource': resource,
'client_id': client_id,
'client_secret': client_secret
}
# The token issuing endpoint
token_url = '{0}{1}'.format(authority, '/{0}/oauth2/token'.format(tenant))
# Perform the post to get access token
response = requests.post(token_url, data=post_data)
try:
# try to parse the returned JSON for an access token
access_token = response.json()['id_token']
return access_token
except:
raise Exception('Error retrieving token: {0} - {1}'.format(
response.status_code, response.text))
谢谢!
【问题讨论】:
-
看起来你正在使用python。您是否有理由不使用ADAL library for Python 在不使用浏览器的情况下获取令牌?
-
感谢您的回答!但我的主要问题不是 Active Directory 令牌,而是 UCWA 令牌,这是不同的。尽管我会尝试使用 ADAL 和 Andrey Markeev 的答案改进我的解决方案。 :+1: