【问题标题】:Accessing SharePoint sites doesn't work in Graph API无法在 Graph API 中访问 SharePoint 网站
【发布时间】:2016-10-19 03:46:08
【问题描述】:

我正在尝试使用 Microsoft graph 访问 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;
                        }
                    }
                }

【问题讨论】:

    标签: office365 office365api microsoft-graph-api office365-restapi


    【解决方案1】:

    我发现了问题,我需要将 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;
                            }
                        }
                    }
                }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-10-09
      • 2019-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      相关资源
      最近更新 更多