Forum Discussion
Trying to pass a Bearer token using the OData.Feed function.
Hello, I'm trying to consume a REST API using the OData.Feed function, I have the URL and the bearer token to perform the request, but I'm getting the error:
"Expression.Error: OData: The header with name 'Headers' has a value type 'Record' that is invalid. Only DateTime, Logical, Number, and Text are supported."
The URL and the bearer token are valid, I can use them to perform a request in post man.
This is the code I'm trying to run (the token and the URL has been censored for security reasons):
let
token = "",
url = "",
//Source = OData.Feed(url, null, [Headers = [Authorization = token] ])
Source = OData.Feed(url, [Headers=[#" Authorization " = token]])
in
SourceThe code has been inspired from the answer found here but the error message seems to be different.
Solved: Re: How authenticate to OData feed that uses Token... - Microsoft Fabric Community
2 Replies
- rubayatyasminCommunity Champion
Hi, ECorona
The error message you're getting suggests that the headers you're passing to the OData.Feed function are not formatted correctly.
The OData.Feed function in Power Query M takes three parameters:
- url (text): The URL of the data feed.
- null or an optional record parameter to specify a relativePath, query, or headers
- options (record) : An optional record parameter that may be specified to control the following options:
- Implementation (text): The implementation of OData to use. The available values are "2.0" and "4.0".
- Query (list): A list of queries to perform on the data feed.
- Headers (record): A record of values to be used as headers in the HTTP request.
The problem seems to be with how you're passing your headers. You need to pass the headers as part of the options record, not the second parameter. The Authorization header usually starts with the word Bearer followed by a space and then the token.
Here's how you can correct your code:
let
token = "Bearer " & "<YourTokenHere>",
url = "<YourUrlHere>",
Source = OData.Feed(url, null, [Headers=[Authorization = token]])
in
SourceYou replace <YourTokenHere> and <YourUrlHere> with your actual token and URL.
The corrected code passes the headers correctly, by including the Bearer keyword before the token. This is the standard way of passing bearer tokens in the Authorization header.
refer:
https://learn.microsoft.com/en-us/powerquery-m/odata-feed
- crln-bluePost Patron
Hello,
I'd like to ask, if I'm gonna use two headers for my odata source, what would the format of the query would be? Is it like this?
Source = OData.Feed(url, null, [Headers=[Authorization = token], [customheader = key])Thank a bunch!