Azure / azure-rest-api-specs-examples

SDK examples for azure-rest-api-specs
MIT License
20 stars 13 forks source link

Python examples in the documentation don't work #1934

Closed bellstar closed 3 months ago

bellstar commented 1 year ago

Link to sample

https://learn.microsoft.com/en-us/rest/api/advisor/recommendations/list?tabs=Python

Library name and version

API Version: 2020-01-01

Language of the Sample

Sample Issue Type

Issue details

When I run the Python example in the reference for this API, it returns a scope error. https://learn.microsoft.com/en-us/rest/api/advisor/recommendations/list?tabs=Python

The environment variables (AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET) and subscriptions are set and running. Any other information on the configuration required to register an application?

C:\Users\user1\source\repos\ListRecommendations\ListRecommendations>python ListRecommendations.py
Traceback (most recent call last):
File "C:\Users\user1\source\repos\ListRecommendations\ListRecommendations\ListRecommendations.py", line 32, in <module>
main()
File "C:\Users\user1\source\repos\ListRecommendations\ListRecommendations\ListRecommendations.py", line 26, in main
for item in response:
File "C:\Users\user1\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\azure\core\paging.py", line 132, in __next__
return next(self._page_iterator)
File "C:\Users\user1\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\azure\core\paging.py", line 76, in __next__
self._response = self._get_next(self.continuation_token)
File "C:\Users\user1\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\azure\mgmt\advisor\operations\_recommendations_operations.py", line 231, in get_next
raise HttpResponseError(response=response, error_format=ARMErrorFormat)
azure.core.exceptions.HttpResponseError: (AuthorizationFailed) The client 'xxxx-xxxx' with object id 'xxxx-xxxx' does not have authorization to perform action 'Microsoft.Advisor/recommendations/read' over scope '/subscriptions/xxxx-xxxx' or the scope is invalid. If access was recently granted, please refresh your credentials.
Code: AuthorizationFailed
Message: The client 'xxxx-xxxx' with object id 'xxxx-xxxx' does not have authorization to perform action 'Microsoft.Advisor/recommendations/read' over scope '/subscriptions/xxxx-xxxx' or the scope is invalid. If access was recently granted, please refresh your credentials.

Expected behavior

I hope that like a "Sample Response" in the documents.

Actual behavior

I write above "Details", I got error.

Reproduction Steps

  1. I register "App registrations" in the Azure AD blade of Azure Portal.
  2. I set the following settings:
  3. I set The environment variables (AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET) and subscriptions in sample code of python from above app of App registrations
  4. Run python code

Environment

Python 3.8 PowerShell 7.2

xboxeer commented 1 year ago

@msyyc would you help check?

msyyc commented 1 year ago

@bellstar according to error message:

(AuthorizationFailed) The client 'xxxx-xxxx' with object id 'xxxx-xxxx' does not have authorization to perform action 'Microsoft.Advisor/recommendations/read' over scope '/subscriptions/xxxx-xxxx' or the scope is invalid. If access was recently granted, please refresh your credentials.

Your subid doesn't have access for Microsoft.Advisor/recommendations/read. I find https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-portal-subscription-admin about how to grant access to sub and hope it is helpful.