2017-09-15 24 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:"エラーコード":[70011]、 "タイムスタンプ": "2017-09-15 12:39 :26Z」、 "trace_id": "17e465ac-9aca-4615から8021-f48ee8f00900"、 "CORRELATION_ID": "47a584ed-07ca-4a51-bdd1-8cb7364de3ee"}invalid_scopeエラーAADSTS70011、なぜ私はこのエラーを取得しています

https://login.microsoftonline.com/common/oauth2/v2.0/authorizeへの呼び出しがある HTTP GET
+0

あなたは文字列として 'FormUrlEncodedContent content'を共有することはできますか? –

+0

"http://microsoft.graph/User.Read"の代わりに "User.Read"という短いスコープを使用してみてください。 –

+0

私もそれを試しました。それでも同じエラーが発生します。 –

答えて

1

POSTではありません。認証コードを受け取り、POSThttps://login.microsoftonline.com/common/oauth2/v2.0/tokenに発行するコールバック関数です。

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

第二段階はPOSTを発行します。

初期GETのプロトタイプが(読みやすくするための新しい行)です。そのプロトタイプは次のとおりです。また、

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のエンドポイントで認証コードのフローを辿りしばらく記事aを書いて、あなたはそれが役に立つかもしれません:Microsoft v2 Endpoint Primer

+0

実用的なコードサンプルはありますか?あなたの記載された手順に従うと、私は最初のリクエストを正常に送信することができます。しかし問題は、ログインページに私を転送するのではなく、html –

+1

私はコードを更新しました。これでリクエストは正常に実行されましたが、ステータスコード302を返す代わりに200を返し、ログインページhtml –

+0

新しい質問にあなたは入りますか?それは別の問題に関連しています。 –

関連する問題