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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

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.

JohnHu
Frequent Visitor

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  

JohnHu
Frequent Visitor

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
Fabric July 2025 Monthly Update Carousel

Fabric Monthly Update - July 2025

Check out the July 2025 Fabric update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.