-
Notifications
You must be signed in to change notification settings - Fork 207
Samples #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Samples #7
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
3 samples
- Loading branch information
commit 9495a2caf738f63fb1ce12ce489c63ca146faeb9
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """ | ||
| The configuration file would look like this: | ||
|
|
||
| { | ||
| "authority": "https://login.microsoftonline.com/organizations", | ||
| "client_id": "your_client_id", | ||
| "secret": "This is a sample only. You better NOT persist your password." | ||
| "scope": ["https://graph.microsoft.com/.default"] | ||
| } | ||
|
|
||
| You can then run this sample with a JSON configuration file: | ||
|
|
||
| python sample.py parameters.json | ||
| """ | ||
|
|
||
| import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1] | ||
| import json | ||
| import logging | ||
|
|
||
| import msal | ||
|
|
||
|
|
||
| # Optional logging | ||
| # logging.basicConfig(level=logging.DEBUG) | ||
|
|
||
| config = json.load(open(sys.argv[1])) | ||
|
|
||
| # Create a preferrably long-lived app instance which maintains a token cache. | ||
| app = msal.ConfidentialClientApplication( | ||
| config["client_id"], authority=config["authority"], | ||
| client_credential=config["secret"], | ||
| # token_cache=... # Default cache is in memory only. | ||
| # See SerializableTokenCache for more details. | ||
| ) | ||
|
|
||
| # The pattern to acquire a token looks like this. | ||
| result = None | ||
|
|
||
| # Firstly, looks up a token from cache | ||
| # Since we are looking for token for the current app, NOT for an end user, | ||
| # notice we give account parameter as None (which is its default value, by the way). | ||
| result = app.acquire_token_silent(config["scope"], account=None) | ||
|
|
||
| if not result: | ||
| # So 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(result["access_token"]) | ||
| print(result["token_type"]) | ||
| print(result["expires_in"]) # You don't normally need to care about this. | ||
| # It will be good for at least 5 minutes. | ||
| else: | ||
| print(result.get("error")) | ||
| print(result.get("error_description")) | ||
| print(result.get("correlation_id")) # You may need this when reporting a bug | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| """ | ||
| The configuration file would look like this: | ||
|
|
||
| { | ||
| "authority": "https://login.microsoftonline.com/organizations", | ||
| "client_id": "your_client_id", | ||
| "scope": ["https://graph.microsoft.com/.default"] | ||
| } | ||
|
|
||
| You can then run this sample with a JSON configuration file: | ||
|
|
||
| python sample.py parameters.json | ||
| """ | ||
|
|
||
| import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1] | ||
| import json | ||
| import logging | ||
|
|
||
| import msal | ||
|
|
||
|
|
||
| # Optional logging | ||
| # logging.basicConfig(level=logging.DEBUG) | ||
|
|
||
| config = json.load(open(sys.argv[1])) | ||
|
|
||
| # Create a preferrably long-lived app instance which maintains a token cache. | ||
| app = msal.PublicClientApplication( | ||
| config["client_id"], authority=config["authority"], | ||
| # token_cache=... # Default cache is in memory only. | ||
| # See SerializableTokenCache for more details. | ||
| ) | ||
|
|
||
| # The pattern to acquire a token looks like this. | ||
| result = None | ||
|
|
||
| # If your device-flow app does not have any interactive ability, | ||
| # you can completely skip the following cache part. Here we demonstrate it anyway. | ||
| # We now check the cache to see if we have some end users signed in before. | ||
| accounts = app.get_accounts() | ||
| if accounts: | ||
| # If so, you could then somehow display these accounts and let end user choose | ||
| print("Pick the account you want to use to proceed:") | ||
| for a in accounts: | ||
| print(a["username"]) | ||
| # Assumeing the end user chose this one | ||
|
||
| chosen = accounts[0] | ||
| # Now let's try to find a token in cache for this account | ||
| result = app.acquire_token_silent(config["scope"], account=accounts[0]) | ||
|
|
||
| if not result: | ||
| # So no suitable token exists in cache. Let's get a new one from AAD. | ||
| flow = app.initiate_device_flow(scopes=config["scope"]) | ||
| print(flow["message"]) | ||
| # Ideally you should wait here, in order to save some unnecessary polling | ||
| # input("Press Enter after you successfully login from another device...") | ||
| result = app.acquire_token_by_device_flow(flow) # By default it will block | ||
|
|
||
| if "access_token" in result: | ||
| print(result["access_token"]) | ||
| print(result["token_type"]) | ||
| print(result["expires_in"]) # You don't normally need to care about this. | ||
| # It will be good for at least 5 minutes. | ||
| else: | ||
| print(result.get("error")) | ||
| print(result.get("error_description")) | ||
| print(result.get("correlation_id")) # You may need this when reporting a bug | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| """ | ||
| The configuration file would look like this: | ||
|
|
||
| { | ||
| "authority": "https://login.microsoftonline.com/organizations", | ||
| "client_id": "your_client_id", | ||
| "username": "your_username@your_tenant.com", | ||
| "scope": ["https://graph.microsoft.com/.default"], | ||
| "password": "This is a sample only. You better NOT persist your password." | ||
| } | ||
|
|
||
| You can then run this sample with a JSON configuration file: | ||
|
|
||
| python sample.py parameters.json | ||
| """ | ||
|
|
||
| import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1] | ||
| import json | ||
| import logging | ||
|
|
||
| import msal | ||
|
|
||
|
|
||
| # Optional logging | ||
| # logging.basicConfig(level=logging.DEBUG) | ||
|
|
||
| config = json.load(open(sys.argv[1])) | ||
|
|
||
| # Create a preferrably long-lived app instance which maintains a token cache. | ||
| app = msal.PublicClientApplication( | ||
| config["client_id"], authority=config["authority"], | ||
| # token_cache=... # Default cache is in memory only. | ||
| # See SerializableTokenCache for more details. | ||
| ) | ||
|
|
||
| # The pattern to acquire a token looks like this. | ||
| result = None | ||
|
|
||
| # Firstly, check the cache to see if this end user has signed in before | ||
| accounts = app.get_accounts(username=config["username"]) | ||
| if accounts: | ||
| # It means the account(s) exists in cache, probably with token too. Let's try. | ||
| result = app.acquire_token_silent(config["scope"], account=accounts[0]) | ||
|
|
||
| if not result: | ||
| # So no suitable token exists in cache. Let's get a new one from AAD. | ||
| result = app.acquire_token_by_username_password( | ||
| config["username"], config["password"], scopes=config["scope"]) | ||
|
|
||
| if "access_token" in result: | ||
| print(result["access_token"]) | ||
| print(result["token_type"]) | ||
| print(result["expires_in"]) # You don't normally need to care about this. | ||
| # It will be good for at least 5 minutes. | ||
| else: | ||
| print(result.get("error")) | ||
| print(result.get("error_description")) | ||
| print(result.get("correlation_id")) # You may need this when reporting a bug | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preferably