Azure Analysis rest api:401未经授权。“身份验证失败。“

2024-04-20 05:33:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试根据此azure文档进行数据分区刷新(post):https://docs.microsoft.com/en-us/azure/analysis-services/analysis-services-async-refresh

不管是用post还是get我都得到了未经授权的401(即使服务关闭了!)。在

我从azure AD(ServicePrincipalCredential)获得令牌。 我将广告添加为analysisservicesadmins(https://docs.microsoft.com/en-us/azure/analysis-services/analysis-services-server-admins) 我把所有者角色赋予了分析服务IAM中的广告。在

它使用具有相同身份验证的analysisservicesmanagementrestapi(https://docs.microsoft.com/en-us/rest/api/analysisservices/operations/list)工作(得到代码响应200)

我的python代码:

from azure.common.credentials import ServicePrincipalCredentials
import requests

credentials = ServicePrincipalCredentials(client_id="ad_client_id",
                                          secret="ad_secret",
                                          tenant="ad_tenant")
token = credentials.token

url = "https://westeurope.asazure.windows.net/servers/{my_server}/models/{my_model}/refreshes"

test_refresh = {
            "Type": "Full",
            "CommitMode": "transactional",
            "MaxParallelism": 1,
            "RetryCount": 1,
            "Objects": [
                {
                    "table": "my_table",
                    "partition": "my_partition"
                }
            ]
        }

header={'Content-Type':'application/json', 'Authorization': "Bearer {}".format(token['access_token'])}

r = requests.post(url=url, headers=header, data=test_refresh)

import json
print(json.dumps(r.json(), indent=" "))

我得到的答复是:

^{pr2}$

我没救了,你能帮我把这件事说清楚吗?在


Tags: httpscomtokenjsondocsmyserviceanalysis
3条回答

您需要资源(访问群体)设置为https://*的令牌。asazure.windows.net

对于令牌验证,我喜欢https://jwt.io

另外,如果你想让它自动化,你有两个选择

  • 通过逻辑应用程序
  • 或者使用Azure数据工厂

这两个我都有非常详细的帖子,如果你想看看

很可能你的代币不对。在

你试过验证你的令牌了吗?使用类似http://calebb.net/

我看到了一些ServicePrincipalCredentials的示例,它们规定了上下文或资源,如下所示:

credentials = ServicePrincipalCredentials(
    tenant=options['tenant_id'],
    client_id=options['script_service_principal_client_id'],
    secret=options['script_service_principal_secret'],
    resource='https://graph.windows.net'

好的样品在这里:

https://www.programcreek.com/python/example/103446/azure.common.credentials.ServicePrincipalCredentials

我认为解决方法是尝试更多有意义的事情,并遵循错误细节。在

最后我解决了这个问题。 我拿错了代币。api需要OAuth2.0身份验证令牌(azureanalysisservicesrestapi文档不太清楚获取令牌的方法)

因为他们会遇到同样的问题,有办法得到一个。在

from adal import AuthenticationContext

authority = "https://login.windows.net/{AD_tenant_ID}"
auth_context = AuthenticationContext(authority)
oauth_token = auth_context.acquire_token_with_client_credentials(resource="https://westeurope.asazure.windows.net", client_id=AD_client_id, client_secret=AD_client_id)
token = oauth_token['accessToken']

相关文档: https://docs.microsoft.com/en-us/python/api/adal/adal.authentication_context.authenticationcontext?view=azure-python#acquire-token-with-client-credentials-resource client-id client-secret-

https://github.com/AzureAD/azure-activedirectory-library-for-python/wiki/ADAL-basics

相关问题 更多 >