1

Microsoftグラフを使用してSharePointサイトにアクセスしようとしていますが、応答メッセージに "Un Authorized"が表示されています。ここでSharePointサイトへのアクセスがグラフAPIで機能しない

は、私は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; 
         } 
        } 
       } 

答えて

3

私は問題を発見し、私は、コンテンツタイプとしてJSONを指定するために、私はすでにリクエストオブジェクトを持っているので、代わりにGetAsyncのSendAsync()を使用する必要があります。

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; 
         } 
        } 
       } 
      } 

希望します。

関連する問題