Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
JesseLil
New Member

Connnecting to the Fabric API with an Entra ID code gives "Access token is invalid" error

Hi!

We have an Entra ID app setup that we use to authorize users to Microsoft Products (Fabric API in this case, but it is also used for Bing Ads API).

 

I am using the following URL to guide the user to authorize the app to access the Fabric API:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=[app_client_id]&response_ty...

This succeeds, and redirects the user to our site:

https://[redirect_url]?code=[code]&session_state=[session_state]#

Next, I try to connect to the Fabric API using this code and cURL:

 

curl https://api.fabric.microsoft.com/v1/workspaces -H "Authorization: Bearer [code]"

The API returns:

{"requestId":"8f49a868-987f-4716-80fd-600edd835419","errorCode":"InvalidToken","message":"Access token is invalid"}

This is rather puzzling, considering that the Entra ID OAuth process succeeds with the scopes without issue. Any idea why this happens?

1 ACCEPTED SOLUTION
TomOs
Resolver I
Resolver I

You're using the authorization code incorrectly as a bearer token. Instead, you need to exchange it for an access token before making API requests. First, send a POST request to the token endpoint and then use it in your API request.

 

Also, make sure your OAuth request includes the necessary scopes for Fabric API, and check that your app registration in Entra ID has the right API permissions with admin consent granted. If you’re still getting the error, verify what response you get when exchanging the code for a token.

View solution in original post

6 REPLIES 6
JesseLil
New Member

That's it! Somehow I managed not to remember this had to be done. Thanks for the solution!

Glad it helped!

TomOs
Resolver I
Resolver I

You're using the authorization code incorrectly as a bearer token. Instead, you need to exchange it for an access token before making API requests. First, send a POST request to the token endpoint and then use it in your API request.

 

Also, make sure your OAuth request includes the necessary scopes for Fabric API, and check that your app registration in Entra ID has the right API permissions with admin consent granted. If you’re still getting the error, verify what response you get when exchanging the code for a token.

Is there an example of this in code?  What is a bearer token vs access token?

Hi, here is how I usually handle the code exchange part in Python:

 

First, swap that auth code for a real token

token_data = {
"client_id": "your_app_id_here",
"client_secret": "your_secret_here", # keep this safe!
"code": "that_code_you_got_from_redirect",
"redirect_uri": "your_callback_url",
"grant_type": "authorization_code",
"scope": "https://api.fabric.microsoft.com/.default"
}

response = requests.post(
"https://login.microsoftonline.com/common/oauth2/v2.0/token",
data=token_data

 

Then grab the good stuff

access_token = response.json()["access_token"]

 

And now try hitting Fabric API

headers = {"Authorization": f"Bearer {access_token}"}
workspaces = requests.get(
"https://api.fabric.microsoft.com/v1/workspaces",
headers=headers

)

This should work  

Thank you!  My problem was the Service Principal was not added to the Fabric Admin Settings.  Once I put the Service Principal (from the App Registration) in a sercurity group, and added the security group email to the Fabric Admin Settings, it worked!

Helpful resources

Announcements
FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Fabric Update Carousel

Fabric Monthly Update - March 2026

Check out the March 2026 Fabric update to learn about new features.