2017-09-15 163 views
1
public string[] scopes1 = new string[] 
{ 
    "https://graph.microsoft.com/User.Read", 
    "https://graph.microsoft.com/User.ReadWrite", 
    "https://graph.microsoft.com/User.ReadBasic.All", 
    "https://graph.microsoft.com/Mail.Send", 
    "https://graph.microsoft.com/Calendars.ReadWrite", 
    "https://graph.microsoft.com/Mail.ReadWrite", 
    "https://graph.microsoft.com/Files.ReadWrite", 

}; 

public async Task<string> GetAccessToken2() 
{ 
    string url = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?";//https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize? 
    using (var client = new HttpClient()) 
    { 
     client.BaseAddress = new Uri(url); 


     // We want the response to be JSON. 
     client.DefaultRequestHeaders.Accept.Clear(); 
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

     // Build up the data to POST. 
     List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>(); 
     postData.Add(new KeyValuePair<string, string>("grant_type", "client_credentials")); 
     postData.Add(new KeyValuePair<string, string>("client_id", appId)); 
     postData.Add(new KeyValuePair<string, string>("client_secret", appPassword)); 
     postData.Add(new KeyValuePair<string, string>("response_type", "code")); 
     postData.Add(new KeyValuePair<string, string>("response_mode", "query")); 
     // postData.Add(new KeyValuePair<string, string>("client_secret", appPassword));    
     //postData.Add(new KeyValuePair<string, string>("client_secret", appPassword)); 
     postData.Add(new KeyValuePair<string, string>("redirect_uri", "http://localhost/5341/Home/AddC")); 
     postData.Add(new KeyValuePair<string, string>("Scope",string.Join(" ", scopes1)));// "openid offline_access https://graph.microsoft.com/mail.read")); 
     postData.Add(new KeyValuePair<string, string>("state", "12345")); 

     FormUrlEncodedContent content = new FormUrlEncodedContent(postData); 

     // Post to the Server and parse the response. 
     HttpResponseMessage response = await client.PostAsync("Token", content); 
     string jsonString = await response.Content.ReadAsStringAsync(); 
     object responseData = JsonConvert.DeserializeObject(jsonString);    

     // return the Access Token. 
     return ((dynamic)responseData).access_token; 
    } 
} 

{ 「錯誤」: 「invalid_scope」, 「ERROR_DESCRIPTION」:「AADSTS70011:本 對輸入參數 '範圍' 提供的值是無效的。範圍 https://graph.microsoft.com/User.Read https://graph.microsoft.com/User.ReadWrite https://graph.microsoft.com/User.ReadBasic.All https://graph.microsoft.com/Mail.Send https://graph.microsoft.com/Calendars.ReadWrite https://graph.microsoft.com/Mail.ReadWrite https://graph.microsoft.com/Files.ReadWrite無效\ r \ nTrace ID: 17e465ac-9aca-4615-8021-f48ee8f00900 \ r \ nCorrelation ID: 47a584ed-07ca-4a51-bdd1-8cb7364de3ee \ r \ n時間戳:2017-09-15 12:39:26Z「,」error_codes「:[70011],」timestamp「:」2017-09-15 12:39 :26Z」, 「trace_id的」: 「17e465ac-9aca-4615-8021-f48ee8f00900」, 「CORRELATION_ID」: 「47a584ed-07ca-4a51-bdd1-8cb7364de3ee」}invalid_scope錯誤AADSTS70011,爲什麼我收到此錯誤

+0

你能分享'FormUrlEncodedContent content'作爲一個字符串? –

+0

我也會嘗試使用短範圍,即「User.Read」而不是「http://microsoft.graph/User.Read」 –

+0

我也試過。並仍然有相同的錯誤。 –

回答

1

https://login.microsoftonline.com/common/oauth2/v2.0/authorize呼叫是HTTP GET,不是POST。這是回收功能,需要授權碼併發出POSThttps://login.microsoftonline.com/common/oauth2/v2.0/token

原型爲初始GET是(對於可讀性新行):

https://login.microsoftonline.com/common/oauth2/v2.0/authorize? 
client_id=[APPLICATION ID]& 
response_type=code& 
redirect_uri=[REDIRECT URI]& 
scope=[SCOPE] 

第二階段發出POST。這原型爲:

POST URL: https://login.microsoftonline.com/common/oauth2/v2.0/token 
POST HEADER: Content-Type: application/x-www-form-urlencoded 
POST BODY: grant_type=authorization_code&code=[AUTHORIZATION CODE]& 
      client_id=[APPLICATION ID]&client_secret=[PASSWORD] 
      &scope=[SCOPE]&redirect_uri=[REDIRECT URI] 

也不是,這不是JSON,該Content-Typeapplication/x-www-form-urlencoded

我寫了一篇文章一個,而通過授權碼流走與V2端點回來,你可能會發現:Microsoft v2 Endpoint Primer

+0

是否有任何工作代碼示例。按照您所述的步驟,我可以成功發送第一個請求。但問題是,而不是轉發我登錄頁面它返回html –

+1

我更新了代碼。現在請求正常運行,但不是返回狀態碼302,而是返回200和登錄頁面html –

+0

您是否可以將新錯誤置於新問題中?它涉及到一個不同的問題。 –

相關問題