gpt4 book ai didi

python - Graph API 以编程方式验证用户身份

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:29 25 4
gpt4 key购买 nike

我正在尝试使用 HTTP POST 请求获取特定用户 OAuth2 不记名 token ,但似乎没有任何效果。

login_url = 'https://login.microsoftonline.com/'
authorize_endpoint = '{0}{1}{2}'.format(login_url,config.tenant_id,'/oauth2/authorize')

bodyvals = {'client_id': config.client_id,
'client_secret': config.client_secret,
'grant_type': 'client_credentials',
'resource':config.resource_endpoint}

return requests.post(authorize_endpoint, data=bodyvals)

上面的代码可以工作,但会代表应用程序生成一个 token 。
我似乎找不到传递用户凭据的方法,也没有任何相关文档。

一般来说,我不在乎答案是用 Python 还是 Powershell 写的,还是只是一般性的解释,我只是似乎不明白如何使用 AAD 正确地做到这一点。

最佳答案

您可以手动完成,请参阅我的其他答案:https://stackoverflow.com/a/40844983/1658906 .

您必须使用 grant_type=password 并调用 oauth2/token 端点。以下是用于身份验证的 C# 版本:

private async Task<string> GetAccessToken()
{
string tokenEndpointUri = Authority + "oauth2/token";

var content = new FormUrlEncodedContent(new []
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", Username),
new KeyValuePair<string, string>("password", Password),
new KeyValuePair<string, string>("client_id", ClientId),
new KeyValuePair<string, string>("client_secret", ClientSecret),
new KeyValuePair<string, string>("resource", PowerBiResourceUri)
}
);

using (var client = new HttpClient())
{
HttpResponseMessage res = await client.PostAsync(tokenEndpointUri, content);

string json = await res.Content.ReadAsStringAsync();

AzureAdTokenResponse tokenRes = JsonConvert.DeserializeObject<AzureAdTokenResponse>(json);

return tokenRes.AccessToken;
}
}

在请求中您必须指定:

  1. 用户名
  2. 密码
  3. 客户端 ID
  4. 客户端 secret
  5. 资源 URI

关于python - Graph API 以编程方式验证用户身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41149609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com