Forum Discussion

thegreatquestio's avatar
thegreatquestio
New Member
1 year ago
Solved

Including the knowbe4 API in data model with BEARER autorization key

Hi, i want to know how to use the knowbe4 api key with powerbi, in the most simple way. 

 

i have tried to use m code in advanced edditor, 

let
// Define API URL
BaseUrl = "https://eu.api.knowbe4.com",
API_Endpoint = "/v1/users", // Modify this for other API endpoints
API_TOKEN = "Bearer eyJ", // Vervang dit met jouw echte API-token


// Set Authorization headers
Headers = [
#"Authorization" = API_TOKEN,
#"Accept" = "application/json"
],

// Make API request with structured Web.Contents
Bron = Json.Document(Web.Contents(BaseUrl, [
RelativePath = API_Endpoint,
Headers = Headers
]))
in
Bron

and with the web include data but only see these options

i use powerBI desktop, and want to get help please it is taking to much time now

  • hi thegreatquestio,

    Thank you for using Microsoft Fabric Community Forum.

     

    Here is a solution for including the authorization key in Power BI to access the KnowBe4 API. This solution addresses the issue you are facing with the authorization key, ensuring that Power BI can properly use it for authentication:

    Open Power BI Desktop and go to Home → Transform Data → Advanced Editor then replace your M code with the following:

     

    Try this M code:

     

    let

        // Define the API base URL and endpoint

        BaseUrl = "https://eu.api.knowbe4.com",  // Adjust the region if necessary (US or EU)

        API_Endpoint = "/v1/users",  // Modify to use the correct endpoint if needed

        API_TOKEN = "Bearer eyJhbGci..." ,  // Replace this with your actual API token

     

        // Set the necessary headers for authentication

        Headers = [

            #"Authorization" = API_TOKEN,

            #"Accept" = "application/json"

        ],

     

        // Make the API request using Web.Contents

        Source = Json.Document(Web.Contents(BaseUrl & API_Endpoint, [

            Headers = Headers

        ])),

     

        // If the response is a list, convert it to a table

        Data = if List.IsEmpty(Source) then

            null

        else

            Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error)

    in

        Data

     

    Privacy Settings: If Power BI doesn't return data, go to File → Options and Settings → Data Source Settings, find the URL, and set the privacy level to Public or Organizational based on your situation.

    Test the Connection: After applying this M code, load the data to see if it fetches the users from the KnowBe4 API successfully.

     

    I hope my suggestions provided valuable insights. If you have any further questions, don’t hesitate to ask in a follow-up message.
    If this post helped, please mark it as "Accept as Solution" so others can benefit as well.

     

    Best regards,
    Sahasra.

8 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi thegreatquestio 

     

    Please try the following steps that may help:
    1. Test the API request in your browser or Postman to ensure that the token and URL are valid
    2. If the returned JSON data is a nested structure, it needs to be expanded into a table, e.g. using Table.FromList(Bron)
    3. Configure Power BI data privacy settings, go to File -> Options and Settings -> Data Source Settings, select the URL of the API, and click “Edit Permissions” to set it to “Organization” or “Public. “”

     

    Best Regards,
    Jarvis Tang
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • thegreatquestio's avatar
      thegreatquestio
      New Member

      Anonymous 

      1 i have tryed the postman it returns 200 so it works well and the url is valid. 
      2 there is nothing wrong with the json powerbi can't accept the api because of the Autorization key

      3 tryed it but is doent work

       

      4 i have tryed to als include it with python code 

      import requests
      import pandas as pd

      # Use the correct API URL
      API_URL = "https://eu.api.knowbe4.com/v1/users" # Change to US if needed

      # Use the same API token from Postman
      API_TOKEN = "eyJhbGci..."

      # My api token is 212 tokens long
      headers = {
      "Authorization": f"Bearer {API_TOKEN}",
      "Accept": "application/json",
      "User-Agent": "PostmanRuntime/7.29.0", # Simulate Postman
      "Cache-Control": "no-cache",
      "Postman-Token": "random-value", # Some APIs check for this
      "Host": "eu.api.knowbe4.com",
      "Connection": "keep-alive"
      }

      # Debug: Print headers
      print("Sending Headers:", headers)

      # Send API request
      response = requests.get(API_URL, headers=headers)
      print(len(API_TOKEN))
      # Print response details
      print("Response Status Code:", response.status_code)
      print("Response Headers:", response.headers)
      print("Response Text:", response.text) # Shows exact API error message

      but this also does not work

      • gblakey's avatar
        gblakey
        New Member

        This is how I connected. Select "New Source" and then "From Web" and click Advanced. Enter the URL and down in the HTTP request header parameters enter Authorization for the value and then "Bearer YourAPIKEY" replace YourAPIKey with the key you generated in KnowBe4. 

        Hope that helps! I'm currently experimenting with creating reports on the training status for managers. 

         

  • v-sgandrathi's avatar
    v-sgandrathi
    Community Support

    hi thegreatquestio,

    Thank you for using Microsoft Fabric Community Forum.

     

    Here is a solution for including the authorization key in Power BI to access the KnowBe4 API. This solution addresses the issue you are facing with the authorization key, ensuring that Power BI can properly use it for authentication:

    Open Power BI Desktop and go to Home → Transform Data → Advanced Editor then replace your M code with the following:

     

    Try this M code:

     

    let

        // Define the API base URL and endpoint

        BaseUrl = "https://eu.api.knowbe4.com",  // Adjust the region if necessary (US or EU)

        API_Endpoint = "/v1/users",  // Modify to use the correct endpoint if needed

        API_TOKEN = "Bearer eyJhbGci..." ,  // Replace this with your actual API token

     

        // Set the necessary headers for authentication

        Headers = [

            #"Authorization" = API_TOKEN,

            #"Accept" = "application/json"

        ],

     

        // Make the API request using Web.Contents

        Source = Json.Document(Web.Contents(BaseUrl & API_Endpoint, [

            Headers = Headers

        ])),

     

        // If the response is a list, convert it to a table

        Data = if List.IsEmpty(Source) then

            null

        else

            Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error)

    in

        Data

     

    Privacy Settings: If Power BI doesn't return data, go to File → Options and Settings → Data Source Settings, find the URL, and set the privacy level to Public or Organizational based on your situation.

    Test the Connection: After applying this M code, load the data to see if it fetches the users from the KnowBe4 API successfully.

     

    I hope my suggestions provided valuable insights. If you have any further questions, don’t hesitate to ask in a follow-up message.
    If this post helped, please mark it as "Accept as Solution" so others can benefit as well.

     

    Best regards,
    Sahasra.

    • v-sgandrathi's avatar
      v-sgandrathi
      Community Support

      Hi thegreatquestio,

       

      Since we haven't heard back from you yet, I'd like to confirm if you've successfully resolved this issue or if you need further help?
      If you've already resolved the issue, you can mark the helpful reply as a "solution" so others know that the question has been answered and help other people in the community. Thank you again for your cooperation!
      If you still have any questions or need more support, please feel free to let us know. We are more than happy to continue to help you.

      • v-sgandrathi's avatar
        v-sgandrathi
        Community Support

        Hi thegreatquestio,

         

        As we did not get a response, may I know if the above reply could clarify your issue, or could you please help confirm if we may help you with anything else?

         

        And if the provided information meets your requirements, you can Accept the solution and also give Kudos on that reply. It helps other users who are searching for this same information and find the information.

         

        Your understanding and patience will be appreciated.