title: Accessing OData Feed from Power Query in Excel: Access to resource is forbidden
body: <p>I have created a locally hosted Web API OData service that I made using a tutorial. It makes use of a database which is also locally hosted and is created via code-first methods. It has two entities in the system, Suppliers and Products. When I access the api from a browser using localhostXXXX/Products or /Suppliersit displays the feed fine like so:</p>
  
  <pre><code>{
    "@odata.context":"http://localhost:64469/$metadata#Products","value":[
      {
        "Id":1,"Name":"Broken Age","Price":15.00,"Category":"Adventure","SupplierId":1
      },{
        "Id":2,"Name":"Final Fantasy IX","Price":10.00,"Category":"JRPG","SupplierId":2
      },{
        "Id":3,"Name":"Fallout 3","Price":15.00,"Category":"Action RPG","SupplierId":3
      }
    ]
  </code></pre>
  
  <p>However when I try to access the feed in Excel via PowerQuery (by going toFrom Other Sources -> From OData Feed) it shows a prompt with the message :"Access to the resource is forbidden".</p>
  
  <p>Can anyone understand why this is? Also how I may get around it? I've done no authentication on the web service and as it works in the web browser its clearly accessable. </p>
  
  <p>Massive thanks to any help anyone can provide.</p>
  
  <p>See below for source.</p>
  
  <p><strong>Controllers</strong></p>
  
  <pre><code>public class SuppliersController : ODataController
      {
          ProductsContext db = new ProductsContext();
  
          protected override void Dispose(bool disposing)
          {
              db.Dispose();
              base.Dispose(disposing);
          }
  
  
          [EnableQuery]
          public IQueryable&lt;Supplier&gt; Get()
          {
              return db.Suppliers;
          }
          [EnableQuery]
          public SingleResult&lt;Supplier&gt; Get([FromODataUri] int key)
          {
              IQueryable&lt;Supplier&gt; result = db.Suppliers.Where(p =&gt; p.Id == key);
              return SingleResult.Create(result);
          }
  
          [EnableQuery]
          public IQueryable&lt;Product&gt; GetProducts([FromODataUri] int key)
          {
              return db.Suppliers.Where(m =&gt; m.Id.Equals(key)).SelectMany(m =&gt; m.Products);
          }
      }
  
  
  public class ProductsController : ODataController
  {
      ProductsContext db = new ProductsContext();
      private bool ProductExists(int key)
      {
          return db.Products.Any(p =&gt; p.Id == key);
      }
      protected override void Dispose(bool disposing)
      {
          db.Dispose();
          base.Dispose(disposing);
      }
  
      [EnableQuery]
      public IQueryable&lt;Product&gt; Get()
      {
          return db.Products;
      }
      [EnableQuery]
      public SingleResult&lt;Product&gt; Get([FromODataUri] int key)
      {
          IQueryable&lt;Product&gt; result = db.Products.Where(p =&gt; p.Id == key);
          return SingleResult.Create(result);
      }
  
      [EnableQuery]
      public SingleResult&lt;Supplier&gt; GetSupplier([FromODataUri] int key)
      {
          var result = db.Products.Where(m =&gt; m.Id == key).Select(m =&gt; m.Supplier);
          return SingleResult.Create(result);
      }
  }
  </code></pre>
  
  <p><strong>Models:</strong></p>
  
  <pre><code>public class Supplier
      {
          public int Id { get; set; }
          public string Name { get; set; }
  
          public ICollection&lt;Product&gt; Products { get; set; }
      }
  
  
  
  public class Product
      {
          public int Id { get; set; }
          public string Name { get; set; }
          public decimal Price { get; set; }
          public string Category { get; set; }
  
          [ForeignKey("Supplier")]
          public int? SupplierId { get; set; }
          public virtual Supplier Supplier { get; set; }
      }
  </code></pre>
  
  <p><strong>Web.Config</strong></p>
  
  <pre><code>&lt;connectionStrings&gt;
      &lt;add name="ProductsContext" connectionString="Data Source=(localdb)\v11.0; 
          Initial Catalog=ProductsContext; Integrated Security=True; MultipleActiveResultSets=True; 
          AttachDbFilename=|DataDirectory|ProductsContext.mdf"
        providerName="System.Data.SqlClient" /&gt;
    &lt;/connectionStrings&gt;
  </code></pre>
  
  <p><strong>WebApiConfig</strong></p>
  
  <pre><code>public static class WebApiConfig
      {
          public static void Register(HttpConfiguration config)
          {
              // New code:
              ODataModelBuilder builder = new ODataConventionModelBuilder();
              builder.EntitySet&lt;Product&gt;("Products");
              builder.EntitySet&lt;Supplier&gt;("Suppliers");
              config.MapODataServiceRoute(
                  routeName: "ODataRoute",
                  routePrefix: null,
                  model: builder.GetEdmModel());
          }
      }
  </code></pre>
  
  <p><strong>Configuration.cs</strong></p>
  
  <pre><code>internal sealed class Configuration : DbMigrationsConfiguration&lt;ProductService.Models.ProductsContext&gt;
  {
      public Configuration()
      {
          AutomaticMigrationsEnabled = false;
          ContextKey = "ProductService.Models.ProductsContext";
      }
  
      protected override void Seed(ProductService.Models.ProductsContext context)
      {
          context.Suppliers.AddOrUpdate(x =&gt; x.Id,
              new Supplier() {Id = 1, Name = "Double Fine" },
              new Supplier() { Id = 2, Name = "Square Enix" },
              new Supplier() { Id = 3, Name = "Bethesda" });
  
          context.Products.AddOrUpdate(x =&gt; x.Id,
              new Product() { Id = 1, SupplierId = 1, Name = "Broken Age", Price=15.00M, Category = "Adventure"},
              new Product() { Id = 2, SupplierId = 2, Name = "Final Fantasy IX", Price = 10.00M, Category = "JRPG" },
              new Product() { Id = 3, SupplierId = 3, Name = "Fallout 3", Price = 15.00M, Category = "Action RPG" });
      }
  }
  </code></pre>
  
  <p><strong>ProductServiceContext.cs</strong></p>
  
  <pre><code>public class ProductsContext : DbContext
  {
      public ProductsContext()
          : base("name=ProductsContext")
      {
      }
      public DbSet&lt;Product&gt; Products { get; set; }
      public DbSet&lt;Supplier&gt; Suppliers { get; set; }
  }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>As asked by Curt Hagenlocher, below is the output if I go to localhost64469/$metadata in a browser:</p>
  
  <pre><code>This XML file does not appear to have any style information associated with it. The document tree is shown below.
  &lt;edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0"&gt;
  &lt;edmx:DataServices&gt;
  &lt;Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="ProductService.Models"&gt;
  &lt;EntityType Name="Product"&gt;
  &lt;Key&gt;
  &lt;PropertyRef Name="Id"/&gt;
  &lt;/Key&gt;
  &lt;Property Name="Id" Type="Edm.Int32" Nullable="false"/&gt;
  &lt;Property Name="Name" Type="Edm.String"/&gt;
  &lt;Property Name="Price" Type="Edm.Decimal" Nullable="false"/&gt;
  &lt;Property Name="Category" Type="Edm.String"/&gt;
  &lt;Property Name="SupplierId" Type="Edm.Int32"/&gt;
  &lt;NavigationProperty Name="Supplier" Type="ProductService.Models.Supplier"/&gt;
  &lt;/EntityType&gt;
  &lt;EntityType Name="Supplier"&gt;
  &lt;Key&gt;
  &lt;PropertyRef Name="Id"/&gt;
  &lt;/Key&gt;
  &lt;Property Name="Id" Type="Edm.Int32" Nullable="false"/&gt;
  &lt;Property Name="Name" Type="Edm.String"/&gt;
  &lt;NavigationProperty Name="Products" Type="Collection(ProductService.Models.Product)"/&gt;
  &lt;/EntityType&gt;
  &lt;/Schema&gt;
  &lt;Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Default"&gt;
  &lt;EntityContainer Name="Container"&gt;
  &lt;EntitySet Name="Products" EntityType="ProductService.Models.Product"&gt;
  &lt;NavigationPropertyBinding Path="Supplier" Target="Suppliers"/&gt;
  &lt;/EntitySet&gt;
  &lt;EntitySet Name="Suppliers" EntityType="ProductService.Models.Supplier"&gt;
  &lt;NavigationPropertyBinding Path="Products" Target="Products"/&gt;
  &lt;/EntitySet&gt;
  &lt;/EntityContainer&gt;
  &lt;/Schema&gt;
  &lt;/edmx:DataServices&gt;
  &lt;/edmx:Edmx&gt;
  </code></pre>