【发布时间】:2018-05-05 05:46:47
【问题描述】:
我正在尝试编写一个简单的控制台应用程序,该应用程序将使用 OAUTH 针对 Azure Graph 进行身份验证,而无需用户名/密码,但在执行 WebClient.DownloadString 方法时收到 403 错误。任何帮助将不胜感激。
static void Main(string[] args)
{
// Constants
var tenant = "mytenant.onmicrosoft.com";
var resource = "https://graph.microsoft.com/";
var clientID = "blah-blah-blah-blah-blah";
var secret = "blahblahblahblahblahblah";
// Ceremony
var authority = $"https://login.microsoftonline.com/{tenant}";
var authContext = new AuthenticationContext(authority);
var credentials = new ClientCredential(clientID, secret);
// Obtain Token
var authResult = authContext.AcquireToken(resource, credentials);
WebClient webClient1 = new WebClient();
webClient1.Headers[HttpRequestHeader.Authorization] = "Bearer " + authResult.AccessToken;
webClient1.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
webClient1.Headers[HttpRequestHeader.Accept] = "application/json";
string payload = webClient1.DownloadString("https://graph.microsoft.com/v1.0/users?$Select=givenName,surname");
}
}
【问题讨论】:
标签: c# rest azure oauth-2.0 microsoft-graph-api