403 Error with MSFT/Power BI Pack

Hi All,

I’m attempting to create a simple pack that queries a Power BI dataset and creates a sync table. I’ve successfully authorized the pack, but when I attempt to query Power BI I receive a 403 error.

I believe one of the potential causes is that the required token is missing from the api call, but I don’t know exactly. I’m using the ‘user owns data’ approach where the app prompts the user to approve its’ permissions.

Is anyone familiar with MSFT APIs? If so, have you come across similar issues?

Initial authorization succeeds
image

Query from the test formula fails

import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();

// Per-user authentication to Microsoft APIs, using OAuth2.
// See https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow
// Allow the pack to make requests to Microsoft.
// pack.addNetworkDomain("microsoft.com");

pack.setUserAuthentication({
  type: coda.AuthenticationType.OAuth2,
   
  authorizationUrl:
    "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
  tokenUrl: "https://login.microsoftonline.com/common/oauth2/v2.0/token",
  scopes: [
    "User.Read",
    "offline_access",
    "https://analysis.windows.net/powerbi/api/Dashboard.Read.All",
    "https://analysis.windows.net/powerbi/api/Report.Read.All",
    "https://analysis.windows.net/powerbi/api/Dataset.ReadWrite.All",
    "https://analysis.windows.net/powerbi/api/Workspace.ReadWrite.All"
   
  ],
  // Additional parameters to ensure a refresh_token is returned.
  additionalParams: {
    prompt: "consent",
  },
  // Enable PKCE (optional but recommended).
  useProofKeyForCodeExchange: true,
  // Determines the display name of the connected account.
  // getConnectionName: async function (context) {
  //   let response = await context.fetcher.fetch({
  //     method: "GET",
  //     url: "https://graph.microsoft.com/v1.0/me",
  //   });
  //   let user = response.body;
  //   return user.displayName;
  // },
  
});

pack.addNetworkDomain("powerbi.com");

// Add formulas here.
pack.addFormula({
  name: "test",
  description: "test connection",
  parameters: [],
  resultType: coda.ValueType.String,
  // connectionRequirement: coda.ConnectionRequirement.Required,
  execute: async function (_, context) {
    let response = await context.fetcher.fetch({
      method: "GET",
      url: "https://api.powerbi.com/v1.0/myorg/groups/{workspace id redacted for this post}/datasets"

    });

    return  response.body
  },
});```

Hi @Chris_Williams - In my experience the Microsoft APIs do tend to be a bit picky when it comes to scopes, and that could be an issue here. You may be able to get more details about error by looking at the HTTP response sent back (click the Show HTTP request details link).

Thanks @Eric_Koleda. I figured out the problem. I used the wrong auth and token urls initially. I believe the ones listed in the Coda Pack documentation work for OneDrive, but if you want to use non-office apps (ie. PowerBi), then you must pull the correct urls from your Azure portal.

1 Like