How do I retrieve a bearer token from a service in Power BI?

喜你入骨 提交于 2020-12-15 06:45:33

问题


I am attempting to call a web API-- say, https://myresources/getresources. This call requires an API Key, resourcesApiKey and a bearer token. The token must be acquired from another service, https://mytokenservice/jwt. It requires an API key, tokenServiceApiKey.

I'm able to get my resource data by manually accessing the token service and entering values, but I'd like to have Power BI take care of all this for me. I read something about this here, but this seems to apply to a static token. Ours changes, so I need to actually call out to this service.

So, what I'm trying to do is this:

  1. Save a token service url and API key with my report. When the report is run, Power BI should use the url and key to retrieve a bearer token.
  2. Save a resources API url and api key with my report. Apply the bearer token retrieved in step 1, along with the resources API key to calls made to the resource API url to retrieve the data I'm really after.

I think the approach described here applies, but I can't get it to work. Here's what I most recently tried using the Power BI advanced editor:

  • I created a parameter in Power BI using Manage Parameters, then used Advanced Editor, I entered the following:

    Web.Content( "https://mytokenservice/jwt", [ ApiKeyName="tokenServiceApiKey" ] )

I also created Power BI parameters for tokenServiceApiKey and resourcesApiKey that contain the key for each.

When I clicked "Done" in the Advanced Editor, though, it automatically generated some code around what I'd written, so that it now reads,

Html.Table(Web.Content("https://mytokenservice/jwt",[ApiKeyName="tokenServiceApiKey"]), {})

What appears in the editor is a table icon containing no data. I should be getting a string back. I don't know where to go from here and am having trouble finding answers online.

Is there anyway to accomplish what I'm after?

I probably haven't explained it very well, so please ask questions if you need more information or clarification.


回答1:


Number-1: You can build your own data connector for your purpose. Some guidelines will find here- Click Here

NUmber-2: You can also use power query to connect your source. Recently I have collected data for clients using oAuth2 API. You can connect to any API using Power/M Query. Following is the sample code for my case, where in the first part I have collected the access_token and in second step, I have collected data using that access token. hope this will give you some idea on connecting to any API using Power Query.

let
    url = "your_authentication_url_here",

    body  = "{ ""client_id"": ""****"",  ""client_secret"": ""****"", ""grant_type"": ""****"", ""audience"":""****""}",
    tokenResponse = Json.Document(Web.Contents(url,[Headers = [#"Content-Type"="application/json"], Content = Text.ToBinary(body) ] )),
    AccessToken = tokenResponse[access_token],
    AccessTokenHeader = "Bearer " & AccessToken,


    data_url = "your_main_url_here",
    data_body = "{
                    ""authorization"": """& AccessTokenHeader & """,
                    ""content-type"": ""application/json""
                }",

    GetGroups = Json.Document(
        Web.Contents(
            data_url, 
            [
                Headers = Json.Document(data_body)
            ] 
        )
    ),    

    categories = GetGroups[categories], --Category here will be changed as per your retrned data

    #"Converted to Table" = Table.FromList(categories, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn
    (
        #"Converted to Table", "Column1", 
        {"ext_id", "shared", "report", "query", "_id", "description"}, --This is column returned from your data set
        {"ext_id", "shared", "report", "query", "_id", "description"}  -- Rename columns accordingly
    )
in
    #"Expanded Column1"


来源:https://stackoverflow.com/questions/63400612/how-do-i-retrieve-a-bearer-token-from-a-service-in-power-bi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!