在Microsoft Graph上设置订阅会导致身份验证错误

时间:2017-12-06 10:47:45

标签: c# microsoft-graph

我正在尝试使用Microsoft Graph设置订阅,但我不断收到以下错误:

{
    "error": {
        "code": "InvalidAuthenticationToken",
        "message": "CompactToken validation failed with reason code: 80049228.",
        "innerError": {
            "request-id": "18c0ce19-cedc-4184-a46f-319e1a350901",
            "date": "2017-12-05T15:59:06"
        }
    }
}

这是我用来做的代码:

HttpWebRequest qboApiRequest = (HttpWebRequest) WebRequest.Create("https://graph.microsoft.com/v1.0/subscriptions");
qboApiRequest.Method = "POST";
qboApiRequest.Headers.Add(string.Format("Authorization: Bearer {0}", token));
qboApiRequest.ContentType = "application/json;charset=UTF-8";
qboApiRequest.Accept = "*/*";

string postData = @"{{""changeType"": ""created,updated"", ""notificationUrl"": ""http://www.somesite.com/response.ashx"", ""resource"": ""me/events"", ""expirationDateTime"": ""2017-12-20T18:23:45.9356913Z"", ""clientState"": ""subscription-identifier""}}";
var encoding = new UTF8Encoding();
var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(postData);
qboApiRequest.ContentLength = bytes.Length;
using(var writeStream = qboApiRequest.GetRequestStream())
{
    writeStream.Write(bytes, 0, bytes.Length);
}

// get the response
string responseText;
HttpWebResponse qboApiResponse = (HttpWebResponse) qboApiRequest.GetResponse();
using(var qboApiReader = new StreamReader(qboApiResponse.GetResponseStream()))
responseText = qboApiReader.ReadToEnd();

我知道令牌是可以的,因为它在其他东西上运行正常,所以我不知道为什么我会收到错误

1 个答案:

答案 0 :(得分:0)

你觉得它在其他东西上运作得怎么样? 访问Microsoft Graph需要订阅您必须拥有calendard.read的webhook。 尝试使用jwt.io检查令牌。您如何注册应用程序。我使用app level auth将其注册到adminconsent。并使用这段代码来生成令牌:

 public static async Task<GenericToken> GetAppToken(string Dom)
        {
            const string Scope = "https://graph.microsoft.com/.default";                      
            string request_content = $"grant_type=client_credentials&scope={Scope}&client_id={ClientId}&client_secret={ClientSecret}";
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"https://login.microsoftonline.com/{Dom}/oauth2/v2.0/token");
            request.Content = new StringContent(request_content, Encoding.UTF8, "application/x-www-form-urlencoded");
            //HttpClient client = new HttpClient();
            HttpResponseMessage response = await staticClient.SendAsync(request);
            return  await response.Content.ReadAsAsync<GenericToken>();

        }