c# - login.microsoftonline 和 restsharp
解决方案
这是 System.Net.HttpClient 的示例。我使用 JSON.NET 来提取令牌,但您可以以任何方式解析结果:
//create a shared client somewhere convenient
HttpClient client = new HttpClient();
...
string tokenurl = "https://login.microsoftonline.com/xxxxxxxxxxxxxxxxxxxxxxx/oauth2/v2.0/token";
string appregClientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string appregClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
HttpRequestMessage tknMsg = new HttpRequestMessage(HttpMethod.Post, tokenurl);
Dictionary<string, string> tknBody = new Dictionary<string, string>{
{ "client_id", appregClientId },
{ "scope", "https://graph.microsoft.com/.default" },
{ "client_secret", appregClientSecret },
{ "grant_type", "client_credentials" }
};
var content = new FormUrlEncodedContent(tknBody);
tknMsg.Content = content;
var tknResponse = await client.SendAsync(tknMsg);
string tknResponseJson = await tknResponse.Content.ReadAsStringAsync();
JObject jobj = JObject.Parse(tknResponseJson);
string accessTkn = (string)jobj["access_token"];
推荐阅读
- javascript - client.on 在单次连接上未获取数据
- python - 如何使用法语 AZERTY 键盘在 VIM 中键入方括号?
- wpf - 当值不等于WPF中的指定值时如何设置触发器?
- c++ - 计算 cpp 周到当前日期时间的秒数,
- python - CV 线性回归 KeyError:“不再支持将列表喜欢传递给带有任何缺失标签的 .loc 或 []
- r - 在 R 中结合来自 Tidymodels 的嵌套和 rolling_origin
- json - 赛普拉斯:json 字段比较的断言错误
- django - 一键更新一组 Django 对象
- flutter - Flutter/Dart 创建新用户时自动创建容器
- android - 无法在 jetpack 数据存储类中写入首选项未找到异常