![](/img/trans.png)
[英]Microsoft Graph API: Httpclient 403 Forbidden error
[英]403 error received when trying to OAUTH authenticate WebClient against Microsoft Azure Graph
我正在嘗試編寫一個簡單的控制台應用程序,它將在不需要用戶名/密碼的情況下使用針對Azure Graph的OAUTH進行身份驗證,但是我在執行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");
}
}
現在已經解決了。 上面的代碼是正確的,但是我缺少一個步驟,那就是在Azure中配置ServicePrincipal:
以下鏈接也非常有用:
https://developer.microsoft.com/zh-cn/graph/docs/concepts/overview (單擊左上方的箭頭以顯示完整列表,然后向下滾動至相應的操作)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.