title: Using Microsoft Graph API to get SharePoint Online team sites
body: <p>I'm trying to get access to SharePoint team sites of an organization. I'm using Microsoft Graph API as it's the most complete API for Office 365. I understand how to get an access token and how to use it to make requests. I know it works because I can get a list of groups but when it comes to get all team sites I get an error message:</p>
  
  <pre><code>Code : invalidRequest    
  Message : Cannot enumerate sites
  </code></pre>
  
  <p>Here is my code. I'm testing it out in a console application.</p>
  
  <pre><code>class Program {
      static void Main(string[] args) {
          Program p = new Program();
          var items = p.GetItems();
          items.Wait();
          foreach (var item in items.Result) {
              Console.WriteLine(item.DisplayName);
          }
  
          Console.ReadLine();
      }
  
      public async Task&lt;IGraphServiceSitesCollectionPage&gt; GetItems() {
          PublicClientApplication myApp =
              new PublicClientApplication("CLIENT-ID-FROM-DEVELOPPER-CONSOLE");
  
          //Gets an access token
          AuthenticationResult authenticationResult =
              await myApp.AcquireTokenAsync(
                  new string[] {
                      "user.read.all",
                      "sites.read.all",
                      "files.read.all",
                      "group.read.all",
                      "Directory.Read.All"
                  }).ConfigureAwait(false);
  
          //Creates the client with the access token
          GraphServiceClient graphClient = new GraphServiceClient(
              new DelegateAuthenticationProvider(
                  async(requestMessage) =&gt; {
                      // Append the access token to the request.
                      requestMessage.Headers.Authorization =
                          new AuthenticationHeaderValue("bearer",
                              authenticationResult.AccessToken);
                  }));
  
          //Requests the site objects
          var sites = await graphClient.Sites.Request().GetAsync();
  
          return sites;
      }
  }
  </code></pre>
  
  <p>I Googled a lot and only found solutions that didn't work for me.</p>