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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
mschoenhub
Frequent Visitor

Cannot apply operator & to types Text and Function

Dear all, 

I am trying to use a bearer token which I get from a function in a query.

Function: 

 

() =>
let
    // Get the API Token
    api_url = "https://api.xxxxxxx.com",
    token_path = "/oauth2/access_token",
    ClientID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 
    Secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 

    EncodedCredentials = "Basic " & Binary.ToText(Text.ToBinary(ClientID & ":" & Secret), BinaryEncoding.Base64),

    Token_Response = Json.Document(Web.Contents(api_url,
    [
        RelativePath = token_path,
        Headers = [#"Content-Type"="application/x-www-form-urlencoded",#"Authorization"=EncodedCredentials],
        Content=Text.ToBinary("grant_type=client_credentials")
    ]
    )
    ),

    // Get the token from the API response
    token = Token_Response[access_token]

in
   Token_Response

 

Query: 

 

let
api_url = "https://api.xxxxxx.com",
path = "/data/v2/libraryEntries",
fields = "all",

Data = Json.Document(Web.Contents(api_url,
[
    RelativePath = path,
    Headers = [#"Authorization"="Bearer " &#"GET Access Token",#"Content-Type"="application/json"]
    ] )),
    #"Converted to Table" = Record.ToTable(Data),
    Value = #"Converted to Table"{2}[Value],
    #"Converted to Table1" = Table.FromList(Value, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table1", "Column1", {"id", "questions", "answer", "languageCode", "creator", "lastUpdatedBy", "createdDate", "lastUpdatedDate", "lastReviewedDate", "lastReviewedBy", "libraryEntryOwner", "inlineImages", "status", "tags", "scores", "location", "attachmentCount"}, {"id", "questions", "answer", "languageCode", "creator", "lastUpdatedBy", "createdDate", "lastUpdatedDate", "lastReviewedDate", "lastReviewedBy", "libraryEntryOwner", "inlineImages", "status", "tags", "scores", "location", "attachmentCount"}),
    #"Expanded lastReviewedBy" = Table.ExpandRecordColumn(#"Expanded Column1", "lastReviewedBy", {"id", "name"}, {"id.1", "name"})
in
    #"Expanded lastReviewedBy"

 

The query throws the following error:

2022-10-08 17_13_56-Window.png

 

 

 

 

I got this from the following tutorial https://www.myonlinetraininghub.com/connecting-to-an-oauth-api-like-paypal-with-power-query where it seems to work. Can someone help me please?

1 ACCEPTED SOLUTION
ImkeF
Community Champion
Community Champion

Ha @mschoenhub ,
that was a tricky one 🙂
In the function for the token, you are not returning the last step.
Please change it to this:

() =>
let
    // Get the API Token
    api_url = "https://api.xxxxxxx.com",
    token_path = "/oauth2/access_token",
    ClientID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 
    Secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 

    EncodedCredentials = "Basic " & Binary.ToText(Text.ToBinary(ClientID & ":" & Secret), BinaryEncoding.Base64),

    Token_Response = Json.Document(Web.Contents(api_url,
    [
        RelativePath = token_path,
        Headers = [#"Content-Type"="application/x-www-form-urlencoded",#"Authorization"=EncodedCredentials],
        Content=Text.ToBinary("grant_type=client_credentials")
    ]
    )
    ),

    // Get the token from the API response
    token = Token_Response[access_token]

in
   token

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

5 REPLIES 5
ImkeF
Community Champion
Community Champion

I would suggest to use a web monitoring tool like Fiddler to compare the 2 requests sent to determine what might be wrong here. 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

ImkeF
Community Champion
Community Champion

Ha @mschoenhub ,
that was a tricky one 🙂
In the function for the token, you are not returning the last step.
Please change it to this:

() =>
let
    // Get the API Token
    api_url = "https://api.xxxxxxx.com",
    token_path = "/oauth2/access_token",
    ClientID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 
    Secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 

    EncodedCredentials = "Basic " & Binary.ToText(Text.ToBinary(ClientID & ":" & Secret), BinaryEncoding.Base64),

    Token_Response = Json.Document(Web.Contents(api_url,
    [
        RelativePath = token_path,
        Headers = [#"Content-Type"="application/x-www-form-urlencoded",#"Authorization"=EncodedCredentials],
        Content=Text.ToBinary("grant_type=client_credentials")
    ]
    )
    ),

    // Get the token from the API response
    token = Token_Response[access_token]

in
   token

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

2022-10-09 14_42_46-Window.png

We are making progress. Thank you very much! In Postman I have access to the resource. Now it could be that I need to specify a scope (scope = "library:read"). How would I do that?

 

But if I insert the token directly then it actually works. So maybe the scope is not the issue. 

ImkeF
Community Champion
Community Champion

Hi @mschoenhub ,
you have to call the function (although it doesn't have any arguments) like so:

let api_url = "https://api.xxxxxx.com",

path = "/data/v2/libraryEntries",

fields = "all",

Data = Json.Document(Web.Contents(api_url,

[ RelativePath = path,

Headers = [#"Authorization"="Bearer " & #"GET Access Token"(),

#"Content-Type"="application/json"] ] )),

#"Converted to Table" = Record.ToTable(Data), Value = #"Converted to Table"{2}[Value],

#"Converted to Table1" = Table.FromList(Value, Splitter.SplitByNothing(), null, null, ExtraValues.Error),

#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table1", "Column1", {"id", "questions", "answer", "languageCode", "creator", "lastUpdatedBy", "createdDate", "lastUpdatedDate", "lastReviewedDate", "lastReviewedBy", "libraryEntryOwner", "inlineImages", "status", "tags", "scores", "location", "attachmentCount"}, {"id", "questions", "answer", "languageCode", "creator", "lastUpdatedBy", "createdDate", "lastUpdatedDate", "lastReviewedDate", "lastReviewedBy", "libraryEntryOwner", "inlineImages", "status", "tags", "scores", "location", "attachmentCount"}), #"Expanded lastReviewedBy" = Table.ExpandRecordColumn(#"Expanded Column1", "lastReviewedBy", {"id", "name"}, {"id.1", "name"})

in

#"Expanded lastReviewedBy"

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Thank you ImkeF, 

it looks like I am getting back the values from the function now but formatting still is an issue: 

2022-10-09 09_37_59-Window.png

 

 

 

 

 

 

 

I got that from this video and it looked so easy there.  https://youtu.be/2RZkc_qrV1g?t=782

Kind regards, Max 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.