# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to the following
# conditions:
# The above copyright notice and this permission notice shall be included in all copies
# or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import msal
import sys
import json
import logging
import requests
import time
if __name__ == '__main__':
print("Starting Client application")
config = json.load(open(sys.argv[1]))
app = msal.ConfidentialClientApplication(
client_id=config["client_id"],
authority="https://login.microsoftonline.com/api.morganstanley.com",
client_credential={
"thumbprint": config["thumbprint"],
"private_key": open(config['private_key_file']).read()
}
)
result = app.acquire_token_silent(config["scope"], account=None)
if not result:
print("No suitable token exists in cache. Let's get a new one from AAD.")
result = app.acquire_token_for_client(scopes=config["scope"])
if "access_token" in result:
print("Calling API.")
# Calling graph using the access token
graph_data = requests.get( # Use token to call downstream service
"https://api.morganstanley.com/hello/v1/services",
headers={'Authorization': 'Bearer ' + result['access_token']}, ).json()
print("Graph API call result: %s" % json.dumps(graph_data, indent=2))
else:
print(result.get("error"))
print(result.get("error_description"))
print(result.get("correlation_id")) # You may need this when reporting a bug
{
"client_id": "ae1d2646-e224-456f-8d2c-4c99521e2d08",
"scope": ["https://api.morganstanley.com/pb/.default"],
"thumbprint": "AA81E8327A50E1F145B789DC691983AEB6CC76CC",
"private_key_file": "A:\\My\\Directory\\Client-instructions\\new-test-keys\\private_key.pem"
}
certifi==2020.11.8
cffi==1.14.3
chardet==3.0.4
cryptography==3.2.1
idna==2.10
msal==1.6.0
pycparser==2.20
PyJWT==1.7.1
requests==2.25.0
six==1.15.0
urllib3==1.26.1