Skip to main content

Notifications

Small and medium business | Business Central, N...
Unanswered

How to receive the correct access token?

Posted on by 5

Im developing a React Expo app where im trying to access a BC page i've created.

Im able to get access to the page in Postman, but i'm unable to get access in my Expo app.

Here is my code:

    async function fetchToken(tenantId, clientId, clientSecret) {
        let token;

        try {
            const response = await fetch(`login.microsoftonline.com/${tenantId}/oauth2/token`, {
                method: "POST",
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                },
                body:
                    "grant_type=client_credentials" +
                    "&scope=">api.businesscentral.dynamics.com/.default" +
                    "&client_id=" + clientId + "&client_secret=" + clientSecret,
            });

            if (!response.ok) {
                throw new Error(`Failed to fetch token: ${response.statusText}`);
            }

            token = await response.json();
            console.log(token);
        } catch (error) {
            console.error(error);
        }

        return token;
    }

TLDR:

I get the wrong access token, can you help me get the correct one?
  • Reinert13 Profile Picture
    Reinert13 5 on at
    RE: How to receive the correct access token?

    I followed you video and got a successful response from Postman, but somehow I still can't get it to work in my app. Using https://jwt.io I can see that the access token from Postman gives access to my tenant, but the access token from my app gives me access to Microsoft Graph.

    This is the response I get in the terminal:

    <error xmlns="docs.oasis-open.org/.../code><message>The credentials provided are incorrect</message></error>

  • Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: How to receive the correct access token?

    Have you added in Azure active directly in BC?

    Also please refer the below link for app registration in Azure.

    www.navwithnav.com/.../

  • Reinert13 Profile Picture
    Reinert13 5 on at
    RE: How to receive the correct access token?

    Do you mean like this?

    const response = await fetch(`login.microsoftonline.com/.../token`, {

                   method: "POST",

                   headers: {

                       "Content-Type": "application/x-www-form-urlencoded",

                   },

                   body:

                       "grant_type=client_credentials" +

                       "&client_id=xxxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxxx" +

                       "&scope=api.businesscentral.dynamics.com/.default" +

                       "&client_secret=xxxxx~xxxxxxxxxxxxxxxxxxxx~xxxxxxxxxxxxx"

               });

    Because that didn't change anything

  • Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: How to receive the correct access token?

    Also Create an entry in Azure Active directory application page in BC and enabled with your custom app.

    Thanks.

  • Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: How to receive the correct access token?

    Please use below syntax for your Body part

    grant_type=client_credentials

    &client_id=413ac35b-e0b6XXXXXXXXXXXXX-72f94d476bf8

    &scope=api.businesscentral.dynamics.com/.default

    &client_secret=9w38Q~PJoLaAsL.ObXXXXXXXXXXXLqIRpGAAb95blu

    header part

    Content-Type - application/x-www-form-urlencoded

  • Jeremy Vyska Profile Picture
    Jeremy Vyska 727 on at
    RE: How to receive the correct access token?

    Not sure if this guide will help, but it may relate?

    businesscentralgeek.com/how-to-use-postman-to-connect-with-business-central-apis

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,232 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,064 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans