Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-在提供正确的身份验证凭据后MS Azure中出现身份验证错误_Python_Azure_Api_Python Requests_Azure Web App Service - Fatal编程技术网

python-在提供正确的身份验证凭据后MS Azure中出现身份验证错误

python-在提供正确的身份验证凭据后MS Azure中出现身份验证错误,python,azure,api,python-requests,azure-web-app-service,Python,Azure,Api,Python Requests,Azure Web App Service,我试图访问azurewebsites.net上的API,但python中的请求如下: print(uncurl.parse( "curl -X GET --header 'Accept: application/json' 'https://pmd-qa-api.azurewebsites.net/api/Portfolios'")) r = requests.get("https://pmd-qa-api.azurewebsites.net/api/Portfo

我试图访问azurewebsites.net上的API,但python中的请求如下:

print(uncurl.parse(
"curl -X GET --header 'Accept: application/json' 'https://pmd-qa-api.azurewebsites.net/api/Portfolios'"))

r = requests.get("https://pmd-qa-api.azurewebsites.net/api/Portfolios",
                 headers={
                     "Accept": "application/json"},
                 auth=('myemail', 'mypass'))
print(r.status_code)
print(r.content)
context = adal.AuthenticationContext('https://login.microsoftonline.com/{tenant-id}')
code = context.acquire_user_code('https://api.example.com', '{client-id}')
print(code['message'])
但我得到错误401:b'您没有权限查看此目录或页面。我输入的是正确的凭据,而不是myemail和mypass。我可以使用相同的凭据登录后通过浏览器访问API。我是否需要在Azure中配置一些东西以使API请求身份验证成功?
TiA以用户身份调用API,使用设备代码流

设备流允许有限的输入体验(例如,电视或很少使用的控制台应用程序)在用户上下文中获得OAuth 2.0访问令牌,同时允许用户在具有更好输入功能的不同设备上(例如,在智能手机或台式机上)执行实际登录

您需要:

  • 在Azure AD中将您的客户端应用注册为本机客户端应用(这一点很重要,因为它会告诉Azure AD这是一个公共客户端,这允许该应用获得具有委托权限的访问令牌,而无需应用验证(因为公共客户端无法对用户保密)
  • 声明您的客户端应用程序需要访问您的API(将注册为单独的web应用程序/web API)
设备代码流包括:

  • 客户端应用程序向Azure AD发出请求以获取设备代码。此设备代码将显示给用户(以及URL)
  • 在单独的设备上(或,例如,在同一设备的完整浏览器中),用户访问给定的URL,并输入给定的设备代码。提示用户登录,并在登录时显示成功消息
  • 同时,客户端应用程序定期轮询Azure AD,查看用户是否已兑换设备代码(并登录)。如果是,客户端应用程序将收到访问令牌
对于Python,使用ADAL for Python同样有用。获取设备代码的请求如下所示:

print(uncurl.parse(
"curl -X GET --header 'Accept: application/json' 'https://pmd-qa-api.azurewebsites.net/api/Portfolios'"))

r = requests.get("https://pmd-qa-api.azurewebsites.net/api/Portfolios",
                 headers={
                     "Accept": "application/json"},
                 auth=('myemail', 'mypass'))
print(r.status_code)
print(r.content)
context = adal.AuthenticationContext('https://login.microsoftonline.com/{tenant-id}')
code = context.acquire_user_code('https://api.example.com', '{client-id}')
print(code['message'])
同样,请查看此问题:


此外,如果您想在本地对其进行测试,则需要登录并获取访问令牌,并将其作为
授权
头添加到请求中。

下面的答案是否有帮助?