Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago

Anonymous access API OAuth2 connection fail in PBI Service

Hi,

 

Data refresh works fine in PBI Desktop but fails in the PBI Service.

My aadToken query:

 

let 
    Source = (TenantId as text, ClientId as text, ClientSecret as text, Resource as text) => 
let 
    aadRequest = [ 
        grant_type = "client_credentials"
        ,client_id = ClientId
        ,client_secret = ClientSecret
        ,resource = Resource
    ], 
    aadContent = Text.ToBinary(Uri.BuildQueryString(aadRequest)), 
    aadTokenRequest = Web.Contents("https://login.microsoftonline.com/", 
        [ 
            Headers = [#"content-type"="application/x-www-form-urlencoded"], 
            Content = aadContent,
            RelativePath=TenantId & "/oauth2/token" 
        ]
    )
    ,aadTokenJSON = Json.Document(aadTokenRequest)
    ,aadToken = aadTokenJSON[access_token] 
in 
    aadToken 
in 
    Source

 

 

 

 

my GetFacilities query:

 

 

 

let 
    Source = (aadToken as text, opcSubscriptionKey as text) => 
    let 
    customersRequest = Web.Contents("https://apis.collabor8.no", 
        [ 
            Headers = [ 
                #"Method"="POST",
                #"Authorization"="Bearer "& aadToken , 
                #"Content-Type"="application/json",
                #"Ocp-Apim-Subscription-Key" = opcSubscriptionKey
            ],
            RelativePath = "/stable/graph",
            Content=Text.ToBinary(
                "{""query"":""query{
                metadata {
                    facilities{
                        name
                        id
                    }
                }
                }""}")
        ] 
    ), 
    #"customers" = Json.Document(customersRequest)
in 
    #"customers"
in 
    Source

 

I call the aadToken inside my data query:

 

let
    Source = aadToken(),
    Custom1 = GetFacilitiesType(Source, "aa03071324654b71166de3cb58d"),
    data = Custom1[data]
in
    data

 

 

It works fine in the PBI Desktop with Anonymous connection, but when I published it to the workspace I got error 404 when I connect to the API anonymously, even though I'm connecting to the root path and already use the relative path.

Any help? Thanks!