2017-02-28 81 views
2

我开发了一个使用UCWA和密码令牌的应用程序。我正在阅读使用事件对应用程序进行身份验证的用户的所有消息,但令牌不会持续很长时间,并且续订使用的是浏览器,这在自动化方面非常糟糕。在Skype For Business Online上自动刷新UCWA令牌

有没有办法让一个令牌不需要通过浏览器进行更新,所以我可以让我的应用程序完全自动化?我已阅读了Github和ucwa网站上的所有文档。

这是我得到令牌的请求。

获取登入URL

DEF get_signin_url(REDIRECT_URI,CLIENT_ID,租户,资源): xframe,user_discovery_uri,资源= do_autodiscover(CONFIG [ '域'])

# 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 

后几个步骤,得到令牌:

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)) 

谢谢!

+0

它看起来像你使用python。有没有理由不利用[ADAL library for Python](https://github.com/AzureAD/azure-activedirectory-library-for-python)获取令牌而不使用broswer? – ShelbyZ

+0

谢谢你的回答!但我的主要问题不在于Active Directory令牌,而是与UCWA令牌不同。虽然我会尝试以防万一我可以使用ADAL和Andrey Markeev的答案改进我的解决方案。 :+1: –

回答

1

尽管文档中出于某种原因说明您需要隐式流,但事实上Skype for Business Online可以与普通的authorization_token + refresh_token方法完美兼容。

我认为他们还没有记录它。由于此方法适用于所有其他Office365 API,因此它很可能不会被删除。

所以您授权用户一次之后,你需要做的 - 正是因为你有你的代码:

POST https://login.microsoftonline.com/common/oauth2/token 
{ 
    grant_type: "authorization_code", 
    code: authorization_code, 
    redirect_uri: redirect_uri, 
    client_id: client_id, 
    client_secret: client_secret 
} 

然后从响应你得到ACCESS_TOKEN和refresh_token。 refresh_token应该保存在某个地方(例如在数据库中)供以后使用。

现在,你用access_token,但总有那么您会收到403,现在你可以使用刷新它的refresh_token以前保存:

POST https://login.microsoftonline.com/common/oauth2/token 
{ 
    refresh_token: refresh_token, 
    grant_type: "refresh_token", 
    redirect_uri: redirect_uri, 
    client_id: client_id, 
    client_secret: client_secret 
} 

经验表明,刷新令牌请求即使进行自access_token过期以来已经过了很长时间。

我在使用Office 365 API的多个应用程序中使用此方法,其中包括与Skype for Business进行交互的应用程序,迄今为止没有任何问题。

在官方文档方面,许多子系统都记录了refresh_token(写作时不适用于Skype for Business),而且在任何地方它的工作原理几乎相同。例如,以下是对Azure的AD这些文件:

+1

嗨!谢谢你的回答,我有一个自动化的解决方案,但它不是最佳的,所以我会尝试你的建议,并让你知道它是否顺利。 谢谢你的帮助! :-) –