我需要在REST中进行以下调用。任何人都可以提供C#样本如何做到这一点? 从: https://docs.microsoft.com/en-us/azure/active-directory/active-directory-protocols-oauth-code
POST /{tenant}/oauth2/token HTTP/1.1
Host: https://login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&client_id=2d4d11a2-f814-46a7-890a-274a72a7309e
&code=AwABAAAAvPM1KaPlrEqdFSBzjqfTGBCmLdgfSTLEMPGYuNHSUYBrqqf_ZT_p5uEAEJJ_nZ3UmphWygRNy2C3jJ239gV_DBnZ2syeg95Ki-374WHUP-i3yIhv5i-7KU2CEoPXwURQp6IVYMw-DjAOzn7C3JCu5wpngXmbZKtJdWmiBzHpcO2aICJPu1KvJrDLDP20chJBXzVYJtkfjviLNNW7l7Y3ydcHDsBRKZc3GuMQanmcghXPyoDg41g8XbwPudVh7uCmUponBQpIhbuffFP_tbV8SNzsPoFz9CLpBCZagJVXeqWoYMPe2dSsPiLO9Alf_YIe5zpi-zY4C3aLw5g9at35eZTfNd0gBRpR5ojkMIcZZ6IgAA
&redirect_uri=https%3A%2F%2Flocalhost%2Fmyapp%2F
&resource=https%3A%2F%2Fservice.contoso.com%2F
&client_secret=p@ssw0rd
答案 0 :(得分:1)
RestSharp确实更容易创建这些休息调用(感谢Soren)。
string token = await GetAccessToken();
var client = new RestSharp.RestClient("https://login.microsoftonline.com/common/oauth2/v2.0");
var request = new RestRequest("token");
request.AddParameter("grant_type","authorization_code");
request.AddParameter("client_id", "edd7c078-...f5d4979c7f4e");
request.AddParameter("code", "Ma49210e1-5332-a5dc-4005-70411bf85fbe");
request.AddParameter("redirect_uri", "https://localhost:44333");
request.AddParameter("client_secret", "qZqxfjca6xg0scsNP67KATy");
request.Method = Method.POST;
request.AddHeader("Authorization", "Bearer " + token);
var response = client.Execute(request);