我正在尝试使用Outlook REST API。我必须通过Azure AD进行身份验证,对此我有一个小问题。当我交换authorization code
对https://login.microsoftonline.com/common/oauth2/v2.0/token的access token
响应时,其中不包含我需要的refresh_token和id_token。我的代码发送请求是
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://login.microsoftonline.com/common/oauth2/v2.0/token");
req.Method = "POST";
req.ServicePoint.Expect100Continue = false;
req.UserAgent = "Example/1.0";
req.ContentType = "application/x-www-form-urlencoded";
using (StreamWriter sw = new StreamWriter(req.GetRequestStream()))
{
string data = "";
data += "grant_type=authorization_code";
data += "&code=" + Request.QueryString("code");
data += "&scope=" + HttpUtility.UrlEncode(string.Join(" ", scopes));
data += "&redirect_uri=" + HttpUtility.UrlEncode(redirectUri);
data += "&client_id=" + appId;
data += "&client_secret=" + appPassword;
sw.Write(data);
}
HttpWebResponse res = req.GetResponse();
using (StreamReader sr = new StreamReader(res.GetResponseStream()))
{
Response.ContentType = "application/json";
Response.Write(sr.ReadToEnd());
Response.End();
}
此代码的响应示例
{
"token_type": "Bearer",
"scope": "https://outlook.office.com/Calendars.Read https://outlook.office.com/Calendars.ReadWrite https://outlook.office.com/Mail.Read",
"expires_in": 3600,
"ext_expires_in": 0,
"access_token": "EwAYA+l3B/Qk ... IpfA0C"
}
我不知道我在做什么与https://oauthplay.azurewebsites.net不同,因为那里的响应包含所有属性。
对于刷新令牌,您必须Offline_Access
在请求授权代码时要求范围,对于id_token,我想您需要Profile
范围。id_token基本上是供特定于应用程序使用的,仅用于验证令牌仅由特定应用程序使用,并且没有其他应用程序代表其他应用程序使用令牌。因此,请尝试请求offline_access
可能为您提供刷新令牌的范围。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句