Protect an API by using OAuth 2.0 with Azure Active Directory and API Management

2024/11/20 19:19:43

I want to Protect amy API by using OAuth 2.0 with Azure Active Directory and API Management. I have added my API in API management and I'm following this article https://learn.microsoft.com/en-in/azure/api-management/api-management-howto-protect-backend-with-aad. This doc uses Azure developer console as sample app client to call the API endpoints but how can I creat my own app which will generate the auth code and I can call my APIs with using that application. I tried using sample apps however I'm new for this so I'm not sure how to move further. So can you please help me with any python or node base code for this? Thanks

Answer

Not sure where you are stuck. If you are a beginner for OAuth 2.0 with Azure Active Directory in Python, you can first dig into the code sample: Python authentication samples for Microsoft Graph, which shows how to use the Microsoft ADAL for Python for authentication. Please pay attention to sample_adal.py file.

@APP.route('/login/authorized')
def authorized():"""Handler for the application's Redirect Uri."""code = flask.request.args['code']auth_state = flask.request.args['state']if auth_state != SESSION.auth_state:raise Exception('state returned to redirect URL does not match!')auth_context = adal.AuthenticationContext(config.AUTHORITY_URL, api_version=None)token_response = auth_context.acquire_token_with_authorization_code(code, config.REDIRECT_URI, config.RESOURCE, config.CLIENT_ID, config.CLIENT_SECRET)SESSION.headers.update({'Authorization': f"Bearer {token_response['accessToken']}",'User-Agent': 'adal-sample','Accept': 'application/json','Content-Type': 'application/json','SdkVersion': 'sample-python-adal','return-client-request-id': 'true'})return flask.redirect('/graphcall')
https://en.xdnf.cn/q/119797.html

Related Q&A

How can filter by list in django

I am trying to filter a queryset by a list I am getting unicode data into format of 1,4,5,6 bycategory = request.GET.getlist(category) print type(category)data = Leads.objects.filter(item_required__id…

Python: get the return code of ant sub-process in windows

I use python to call ant, I want to get the return code of the ant for detect ant error.for example, in cmd.exe, C:\Documents and Settings\Administrator>ant sfsf Buildfile: build.xml does not exist!…

Skipp the error while scraping a list of urls form a csv

I managed to scrape a list of urls from a CSV file, but I got a problem, the scraping stops when it hits a broken link. Also it prints a lot of None lines, is it possible to get rid of them ? Would ap…

Getting the TypeError - int object is not callable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Reordering columns in CSV

Question has been posted before but the requirements were not properly conveyed. I have a csv file with more than 1000 columns:A B C D .... X Y Z 1 0 0.5 5 .... 1 7 6 2 0 0.6 4 …

Variable not defined in while loop in python?

I am trying to write a simple program in python to read command line arguments and print a final word based on the arguments. If there is any argument of the form "-f=" then the will go to t…

Hours and time converting to a certain format [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

Python socket server: listening to multiple clients [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

I have a problem with encoding with russian language for my python script [duplicate]

This question already has answers here:UnicodeEncodeError: ascii codec cant encode character u\xa0 in position 20: ordinal not in range(128)(34 answers)Closed last year.I am trying to send an email fro…

how do you style data frame in Pandas

I have this data frame: dfServer Env. Model Percent_Utilized server123 Prod Cisco. 50 server567. Prod Cisco. 80 serverabc. Prod IBM. 100 serverdwc.…