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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
marcio_fornari
Resolver I
Resolver I

Right way to get Embedded Token

Hello,

Today i'm using this code in PHP to get a Embedded Token each time that i need view the reports. 

$client = new \GuzzleHttp\Client();
        $url = 'https://login.windows.net/' . $directoryIdAzure . '/oauth2/token';
        try {
            /** @var GuzzleHttp\Client $client **/
            $response = $client->post(
               
                $url,
                [
                    "headers" => [
                        "Accept" => "application/json"
                    ],
                    'form_params' => [
                        'resource'      => 'https://analysis.windows.net/powerbi/api',
                        'client_id'     => $clientIdAzure,
                        'client_secret' => $clientSecretAzure,
                        'grant_type'    => 'password',
                        'username'      => $userPowerBI,
                        'password'      => $passPowerBI,
                        'scope'         => 'openid',
                    ]
                ]
            );

            $body = json_decode($response->getBody()->getContents(), true);
            //GET THE TOKEN
            $token = $body['access_token'];

It's work but, i don't know if this is right way to get the token, because at Azure Portal show me many logins each time that token Generated..
login.png

 


i'm lookin for this API: https://api.powerbi.com/v1.0/myorg/GenerateToken but i don't know use this.

 

Wich is the right way to generate the Token?

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

HI @marcio_fornari,

I think these should more relate to your website design, power bi embedded token has its own expiration time. (stored in expiration field)  Embed Token - Generate Token#embedtoken 

You did not need to invoke generate token API every time you access these contents. BTW, your functions also triggered if web page refreshed or other functions which include multiple threads processing.

I'd like to suggest you create a function to invoke get token api and save token string and expiration date time to session. It should contain if statement to check the session if the token exists or expired to generate a new token or return current token.

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

2 REPLIES 2
v-shex-msft
Community Support
Community Support

HI @marcio_fornari,

I think these should more relate to your website design, power bi embedded token has its own expiration time. (stored in expiration field)  Embed Token - Generate Token#embedtoken 

You did not need to invoke generate token API every time you access these contents. BTW, your functions also triggered if web page refreshed or other functions which include multiple threads processing.

I'd like to suggest you create a function to invoke get token api and save token string and expiration date time to session. It should contain if statement to check the session if the token exists or expired to generate a new token or return current token.

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hi @v-shex-msft, it's a good ideia save the Token and the Expiration time.

Thank you.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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