UCWA とパスワード トークンを使用してアプリケーションを開発しました。イベントを使用してアプリケーションで認証されたユーザーに送信されるすべてのメッセージを読み取っていますが、トークンは長くは続かず、更新はブラウザーを使用しているため、自動化に関してはひどいです。
アプリケーションを完全に自動化できるように、ブラウザ経由で更新する必要のないトークンを取得する方法はありますか? Github と ucwa Web サイトのすべてのドキュメントを読みました。
これは、トークンを取得するために私が行う要求です。
サインイン URL を取得する
def get_signin_url (redirect_uri、client_id、tenant、resource): 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
いくつかの手順の後、トークンを取得します。
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))
ありがとうございました!