Forum Discussion

aldodfm's avatar
aldodfm
Frequent Visitor
10 years ago
Solved

Anonymous access

Hello @Community,

 

I have a power query with a REST api call:

 

 authKey = "Bearer blablablaKey",
 url = "https://blablablaendpoint.com/api/v1/usage/2016",

 GetJsonQuery = Web.Contents(url,
     [
         Headers = [#"Authorization"=authKey, #"Content-Type"="application/json"]
     ]
 ),
FormatAsJsonQuery = Json.Document(GetJsonQuery),

So, this power query use "Anonymous" authentication. When i refresh this query in Power BI Desktop appears credentials error, but if you accept the power query is executed and everything is ok.

But when i try to schedule refresh in Power BI service appears the credentials problems but doesn't exists a way to say "execute anyway".

 

Is like Power BI service checks the anonymous access to the URL and if it doesn't work, power bi doesn't try to execute the power query script.

 

 

Please, there is something to do? Power BI team will do something?

 

Thx in advance.

  • fso's avatar
    fso
    10 years ago

    I had the same issue and logged a support ticket with Microsoft because I wanted to build my own Google Analytics queries (include filters, segments, etc).

    Here's the answer I got from the support team:
    This is not possible due to constraints with the way Web.Contents stores credentials. They are stored based upon the URL value passed, and there can only be one URL per Dataset. In this case, you're trying to short-circuit a typical oAuth2 flow (one URL) and call the API (another URL). I made an attempt at using RelativePath to 'trick' the service, but stills require that the root of those two endpoints returned a valid HTTP 200 response to indicate connection success. In this case, https://www.googleapis.com returns a 404. Unfortunately, therefore it is not possible as an uploaded PBIX file. I suggest creating a new issue describing what data you would want to see in the Google Analytics content pack (https://app.powerbi.com/groups/me/getdata/services/google-analytics) and we can see about adding it to the model.

    The only workaround for me so far, is to execute the query elsewhere (for me it's Google Spreadsheet) and connect PBI to that as a data source.

     

33 Replies

  • Hi

     

    I have this exact same issue.

     

    The "authentication" is baked into the Power Query code itself through the API key, so setting the authentication method to anonymous works perfectly in Power BI desktop. However I get the same invalid credentials errors when trying to refresh from Power BI.

     

    My code is similar:

    Source = Json.Document(Web.Contents("https://myapi.com/api/method", [Headers=[#"X-API-Key"="Some_GUID"]])),

     

    If anyone can come up with a workaround for this, I'd be very grateful as well!

     

    Duncan

     

    EDIT:

    Just to note, anonymous authentication works fine at the root level of the api (https://myapi.com/api) and simply lists the methods. However in the Power BI refresh, it tries to load the method url which of course fails without the api key.

    • fso's avatar
      fso
      Advocate II

      I had the same issue and logged a support ticket with Microsoft because I wanted to build my own Google Analytics queries (include filters, segments, etc).

      Here's the answer I got from the support team:
      This is not possible due to constraints with the way Web.Contents stores credentials. They are stored based upon the URL value passed, and there can only be one URL per Dataset. In this case, you're trying to short-circuit a typical oAuth2 flow (one URL) and call the API (another URL). I made an attempt at using RelativePath to 'trick' the service, but stills require that the root of those two endpoints returned a valid HTTP 200 response to indicate connection success. In this case, https://www.googleapis.com returns a 404. Unfortunately, therefore it is not possible as an uploaded PBIX file. I suggest creating a new issue describing what data you would want to see in the Google Analytics content pack (https://app.powerbi.com/groups/me/getdata/services/google-analytics) and we can see about adding it to the model.

      The only workaround for me so far, is to execute the query elsewhere (for me it's Google Spreadsheet) and connect PBI to that as a data source.

       

      • DuncanP's avatar
        DuncanP
        Advocate II

        Well that's actually fixed it for me, so thanks!

         

        I've modified my json call to:

        Source = Json.Document(Web.Contents("https://myapi.com", [Headers=[#"X-API-Key"="Some_GUID"], 
        RelativePath="/api/​method"])),

        which now allows it to authenticate properly. In this case I'm lucky that the root url of the api works with anonymous access.

         

         

        Duncan

  • I just thought I'd add my work-around to this thread...it's not ideal, but work-arounds never are :)

     

    Because I'd prefer not to hard-code my API credentials within the PBIX file, I put them in a plain text file, and upload it to OneDrive.  I then connect to the text file from PBI to pull in the credentials.  In my case, I'm connecting to the BridgeEdge API.

     

    Parameters:

    • BrightEdge Credentials File - URL to the OneDrive file (eg. "http://.../MyCredentials.txt") containing the credentials, in the format "username:password"
    • BrightEdge Account - required by API--an ID of the account to query data for
    • BrightEdge Query - required by API--the BQL query (text that needs to be POSTed in the BODY)

    Queries:

    • BrightEdgeData 
    let
        URL = "https://api.brightedge.com",
        Body = #"BrightEdge Query",
        Options = [
            Headers = [
                Authorization = "Basic " & Binary.ToText(Web.Contents(#"BrightEdge Credentials File"))
            ],
            RelativePath = "/3.0/query/" & #"BrightEdge Account",
            Content = Text.ToBinary(Body),
            Timeout = #duration(0,0,5,0)
        ],        
        Source = Web.Contents(URL, Options),
        #"Imported JSON" = Json.Document(Source,65001),
        values = #"Imported JSON"[values],
        #"Converted to Table" = Table.FromList(values, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"domain", "rank_p1"}, {"domain", "rank_p1"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Expanded Column1",{{"domain", type text}, {"rank_p1", Int64.Type}})
    in
        #"Changed Type"

    Hopefully this technique is helpful to someone else :)

    • chriswragge's avatar
      chriswragge
      Helper I

      jeffshieldsdev - thanks for the good idea about keeping credentials and the body out of PowerBI.

      One question though - when you load these external file into PowerBI, how do you get them read as Text? 

       

      For example, my 'body' is in Json format (multiple rows), but saved as a .txt. It reads in as a Table and I can convert to List. However when i try to use it in the main query, I get thrown an error.

       

      Expression.Error: We cannot convert a value of type Table to type Text
      • chriswragge's avatar
        chriswragge
        Helper I

        @jeffshieldsdev - All good. Solved my question.

         

        Load .txt file in a seperate query only using: 

        = File.Contents("C:\Users\.... 

         

        Then in the main query, reference this via:

        body = Text.FromBinary(myFile,1252),

         

  • aldodfm's avatar
    aldodfm
    Frequent Visitor

    Any help from Microsoft? How i can contact with them?

  • Good afternoon!!!
    I saw many websites, chats and posts, but few are objective and each person values his method as if it were unique !!!
    Each has its logic, so there is no single method !!
    Objectively, for me ...
    It worked this way:

    Enjoy: 😘

    ===========================
    let
    tenantId = "12345678-9012-3456-7890-123456789012",
    clientId = "abcdefgh-9012-3456-7890-123456789012",
    clientSecret = "1234567890qwertyuiopasdf",
    grantType = "client_credentials",
    resource = "https://graph.windows.net/",
    endpointUsers = "/users",
    apiVersion = "?api-version=1.6",

    baseURL="https://login.microsoftonline.com",
    relativePath = "/"&tenantId&"/oauth2/token",
    urlToken = baseURL & relativePath,

    body = [client_id=clientId,
    grant_type=grantType,
    client_secret=clientSecret,
    resource=resource],

    //Access
    Source = Json.Document(Web.Contents(urlToken, [Headers=[#"Accept" = "application/json", #"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"],Content = Text.ToBinary(Uri.BuildQueryString(body))])),
    accessToken = Source[access_token],
    tokenType = Source[token_type],
    fullAccessToken = tokenType&" "&accessToken,

    //Get Users
    SourceUsers = OData.Feed(resource & tenantId & endpointUsers, [Authorization=fullAccessToken], [Implementation="2.0", Query=[#"api-version"="1.6"]]),

    Step1 = Table.RemoveColumns(SourceUsers,{"createdObjects", "department", "employeeId", "facsimileTelephoneNumber", "legalAgeGroupClassification", "jobTitle", "telephoneNumber", "mobile", "givenName", "physicalDeliveryOfficeName", "consentProvidedForMinor", "sipProxyAddress", "streetAddress", "city", "state", "country", "postalCode", "ageGroup", "companyName", "preferredLanguage", "manager", "directReports", "members", "transitiveMembers", "owners", "ownedObjects", "appRoleAssignments", "licenseDetails", "oauth2PermissionGrants", "ownedDevices", "registeredDevices", "assignedLicenses", "assignedPlans", "deletionTimestamp", "immutableId", "isCompromised", "lastDirSyncTime", "passwordProfile", "provisionedPlans", "provisioningErrors", "proxyAddresses", "signInNames", "usageLocation", "userIdentities", "memberOf", "transitiveMemberOf", "thumbnailPhoto", "createdOnBehalfOf", "dirSyncEnabled", "onPremisesDistinguishedName", "onPremisesSecurityIdentifier", "showInAddressList"}),
    Step2 = Table.TransformColumnTypes(Step1,{{"createdDateTime", type datetime},{"refreshTokensValidFromDateTime", type datetime}}),
    Step3 = Table.TransformColumns(Step2, {"otherMails", each Text.Combine(List.Transform(_, Text.From)), type text})
    in
    Step3