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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
JohNWMCC
Regular Visitor

Replacing / Renewign API Token

Hi Folks,

I'm very new to BI and the whoel connecting API's to it. I ahve managed to connect an API from our supplier and pull in the data to Power Bi by using the Get Data > Web option and so can add the beaerer token

 

Thats' all worked and I have the data in the system but I have now found out the API token needs to be renewed every 30 days and I have no idea how to do this automatically. I have manged to generate a new token but can't see where I can go to manually update this.

 

Here's my main questions:

1. How the heck do I update the berarer token

2. is ther e away to automate getting the updated token every 30 days? They do have a Post option for authentication and a Get option but no idea how to use those so keen for some pointers for readign material

5 REPLIES 5
JohNWMCC
Regular Visitor

Hi @v-shex-msft 

Thansk for your reply. I guess the biggest challenge I have right off the bat is I am a complete newbe at all this and really don't fully understand what your code does.

 

At the moment when setting up I'm dragging the data in via the Get Data > Web

 

The API doens't have an option for /gettoken

We ahve the ​/api​/auth​/authenticate option which we inset a username & password to and this returns the token code. I'm then insertign this into the HTTP request header parameters for the advanced Get Data from web.

 

so I don't get any Secret ID for example

 

HI @JohNWMCC,

It should be helpful if you share the document of the API usages, we can create the sample code based on them.

How to Get Your Question Answered Quickly  

Regards,

Xiaoxin Sheng

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

Does this help?

 

Authorised requests to the API should use an Authorisation header with the value Bearer <TOKEN>, where <TOKEN> is an access token obtained through the authorisation flow. The <TOKEN> is only valid for the same IP that was originally used to acquire the token

 

Auth
/api/auth/authenticate


DESCRIPTION
This endpoint is used to acquire a bearer token authorisation token. URL STRUCTURE
https://api.XXXXXX.com/api/auth/authenticate

METHOD: POST

EXAMPLE:
{
"username": "user",
"password": "password"
}

PARAMETERS
username String(min_length=8, required) The username for the user account who has the API access.
password String(min_length=8, required) The password for the user account who has the API access.

 

RETURNS

{
"token": "eyJhbGciOiJIUzI1NixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpZT2hnYzJHNE1YYmFuV21KMGt3NWw4bFBUU0p2RzFlSDVTb09iZn.e6I1b-Sp0CMhrt3Ah1cAEDVVJrDzmBbGGCg",
"message": null,
"username": "username",
"lastName": "One",
"firstName": "User",
"emailAddress": “email@email.com”,
"guid": "ABCDEFGH-1234-5678-9012-ABCDEFGHIK"
}

token String The authorisation token
message String Any message returned
username String The authorised user’s username
lastName String The authorised user’s last name
firstName String The authorised user’s first name
emailAddress String The authorised user’s email address
guid String The authorised user’s guid

 

Errors

Code 400

Description: Bad Request. The respond body is a plaintext message with more information. { "message": "Username or password is incorrect” }

HI @JohNWMCC,

It sounds like the auth 'user/pwd' parts are not sent from the header, you can try to use the following code to use the 'content' option to post requests to get the token:

let
    accesstoken = "xxxxx",
    rootURL = "https://api.XXXXXX.com",
    body = "{'username': 'user','password':'password'}",
    authPath = "/api/auth/authenticate",
    GetJson =
        Web.Contents(
            rootURL,
            [
                Headers = [
                    Authorization = "Bearer " & accesstoken,
                    #"Content-Type" = "application/json"
                ],
                RelativePath = authPath,
                Content = Text.ToBinary(body)
            ]
        ),
    token = Json.Document(GetJson)[token],
    //next operations
    Result =
        Web.Contents(
            rootURL,
            [
                Headers = [
                    Authorization = "Bearer " & token,
                    #"Content-Type" = "application/json"
                ],
                RelativePath = "/api/xxxx/xxxx"
            ]
        )
in
    Result

Regards,

Xiaoxin Sheng

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

HI @JohNWMCC,

You can add the get token step into the query table and parameterize the get data step with this variable, then this token will be updated every time the data source refresh.

Sample code:

 

let
    //get token
    secretId = xxxxx,
    authKey = "Basic " & secretId,
    url = "https://api.xxxxx.com/xxxxx/gettoken",
    GetJson =
        Web.Contents(
            url,
            [
                Headers = [
                    #"Authorization" = authKey,
                    #"Content-Type" = "application/x-www-form-urlencoded;charset=UTF-8"
                ]
            ]
        ),
    token = Json.Document(GetJson)[access_token],
    //get data
    Result =
        Web.Contents(
            "https://xxxxx.xxx.com",
            [
                Headers = [
                    #"Content-Type" = "application/json",
                    Authorization = "Bearer " + token
                ],
                RelativePath = "/xxxxx/xxxxx"
            ]
        )
in
    Result

 

Regards,

Xiaoxin Sheng

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

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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