虽然已经晚了两年回答,但我认为这对未来的使用会有帮助。
一旦您注册了应用程序,您可以获取访问令牌 无需弹出窗口,请参见此帖子“使用OAuth REST API获取Microsoft Graph访问令牌”,其中介绍了如何获取令牌(以Postman示例为例)。 我根据这个Postman示例编写了以下代码:
使用C#语言获取无弹窗口访问令牌示例:
public async Task GetTokenAsync(string tenant, string clientId, string clientSecret, string username, string password)
{
HttpResponseMessage resp;
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
var req = new HttpRequestMessage(HttpMethod.Post, $"https://login.microsoftonline.com/{tenant}/oauth2/token/");
req.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"grant_type", "password"},
{"client_id", clientId},
{"client_secret", clientSecret},
{"resource", "https://graph.microsoft.com"},
{"username", username},
{"password", password}
});
resp = await httpClient.SendAsync(req);
string content = await resp.Content.ReadAsStringAsync();
var jsonObj = new JavaScriptSerializer().Deserialize<dynamic>(content);
string token = jsonObj["access_token"];
Console.WriteLine(token);
}
}
一旦你拥有了令牌,就很容易签到。