首页 文章

访问SharePoint网站在Graph API中不起作用

提问于
浏览
1

我试图使用Microsoft图访问SharePoint网站,我在响应消息中收到“ Un Authorized ” .

以下是我想要获取SP网站的信息:https://graph.microsoft.com/beta/sharePoint/sites

该查询在图浏览器中工作,但不能通过我的客户端代码 .

注意:该应用程序具有SharePoint Online和Microsoft Graph的完全委派权限 .

我尝试从我的应用程序调用其他SharePoint资源,我仍然得到未经授权的错误消息 .

码:

using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/beta/sharePoint/sites"))
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                    using (HttpResponseMessage response = await client.GetAsync("https://graph.microsoft.com/beta/sharePoint/sites"))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            msgResponse.Status = SendMessageStatusEnum.Sent;
                            msgResponse.StatusMessage = await response.Content.ReadAsStringAsync();
                        }
                        else
                        {
                            msgResponse.Status = SendMessageStatusEnum.Fail;
                            msgResponse.StatusMessage = response.ReasonPhrase;
                        }
                    }
                }

1 回答

  • 3

    我发现了这个问题,我需要将JSON指定为内容类型并使用SendAsync而不是GetAsync(),因为我已经有了一个请求对象 .

    using (var client = new HttpClient())
                {
                    using (var request = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/beta/sharePoint/sites"))
                    {
                        request.Headers.Accept.Add(Json);
                        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                        using (HttpResponseMessage response = await client.SendAsync(request))
                        {
                            if (response.IsSuccessStatusCode)
                            {
                                msgResponse.Status = SendMessageStatusEnum.Sent;
                                msgResponse.StatusMessage = await response.Content.ReadAsStringAsync();
                            }
                            else
                            {
                                msgResponse.Status = SendMessageStatusEnum.Fail;
                                msgResponse.StatusMessage = response.ReasonPhrase;
                            }
                        }
                    }
                }
    

    希望这可以帮助 .

相关问题