尝试在MS Azure中获取令牌时,获取了无效的客户端密码、缺少参数client\u secret、client\u断言

2024-06-01 06:43:08 发布

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

我的目标是获取令牌,然后查询图形资源

我写这些代码是为了从我的应用程序中获取令牌。第一个代码,使用登录/通行证获取令牌:

from adal import AuthenticationContext

auth_context = AuthenticationContext('https://login.microsoftonline.com/g***************r.onmicrosoft.com')

token = auth_context.acquire_token_with_username_password('https://graph.microsoft.com',
                                                       't***************n.fr',
                                                       '***************', 'de8bc8b5-***************-b748da725064')

得到了这些:

adal.adal_error.AdalError: Get Token request returned http error: 401 and server response: {"error":"invalid_client","error_description":"AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.\r\nTrace ID: e9ffa752-a258-4e32-9164-156e50912a00\r\nCorrelation ID: afb3c52f-8f36-4904-9926-90ba2aaba0ab\r\nTimestamp: 2020-12-08 14:02:58Z","error_codes":[7000218],"timestamp":"2020-12-08 14:02:58Z","trace_id":"e9ffa752-a258-4e32-9164-156e50912a00","correlation_id":"afb3c52f-8f36-4904-9926-90ba2aaba0ab","error_uri":"https://login.microsoftonline.com/error?code=7000218"}

第二个代码,获取具有客户端凭据的令牌:

from adal import AuthenticationContext

auth_context = AuthenticationContext('https://login.microsoftonline.com/g*************r.onmicrosoft.com')

token = auth_context.acquire_token_with_client_credentials('https://graph.microsoft.com',
                                                       'de8bc8b5-************-b748da725064',
                                                       '****************************')

得到了这些:

adal.adal_error.AdalError: Get Token request returned http error: 401 and server response: {"error":"invalid_client","error_description":"AADSTS7000215: Invalid client secret is provided.\r\nTrace ID: 79fe51c5-2987-4855-acf5-88fee0592a00\r\nCorrelation ID: cdfc9215-b7a6-4ceb-9d90-2de371c3178c\r\nTimestamp: 2020-12-08 14:21:13Z","error_codes":[7000215],"timestamp":"2020-12-08 14:21:13Z","trace_id":"79fe51c5-2987-4855-acf5-88fee0592a00","correlation_id":"cdfc9215-b7a6-4ceb-9d90-2de371c3178c","error_uri":"https://login.microsoftonline.com/error?code=7000215"}

我不知道客户的秘密是错的还是我做了什么


Tags: 代码httpscomclienttokenauthidrequest
1条回答
网友
1楼 · 发布于 2024-06-01 06:43:08

我不需要那些LIB。
Lib请求就足够了

def authenticate_oauth():
    validation_url = 'https://login.microsoftonline.com/******************.com/oauth2/v2.0/token'
    client_id = '1a******************************b7'
    client_secret = '-*****************************r'
    grant_type = 'client_credentials'
    scope = 'https://graph.microsoft.com/.default'
    params = {
        'client_id': client_id,
        'client_secret': client_secret,
        'grant_type': grant_type,
        'scope': scope,
    }
    response = requests.post(validation_url, data=params)

    return json.loads(response.content)


if __name__ == '__main__':
    token = authenticate_oauth()
    if 'access_token' in token:
        graph_data = requests.get(
        'https://graph.microsoft.com/v1.0/users',
        headers={'Authorization': 'Bearer ' + token['access_token']})
        data_users = json.loads(graph_data.content)
        for data in data_users['value']:
            print(data)

相关问题 更多 >