【问题标题】:Refresh UCWA token automatically on Skype For Business Online在 Skype For Business Online 上自动刷新 UCWA 令牌
【发布时间】: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:

标签: skype-for-business ucwa


【解决方案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 的此类文档:

【讨论】:

  • 嗨!谢谢你的回答,我得到了一个自动化的解决方案,但它不是最佳的,所以我会尝试你的建议,如果进展顺利,我会告诉你。感谢您的帮助! :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 2018-05-15
相关资源
最近更新 更多