简体   繁体   中英

How to call this REST - Post resource in C#

I need to make the following call in REST. Can anyone provide a C# sample on how to do this ? From: 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

RestSharp really did make creating these rest calls easier (thanks 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM