Forum Discussion
Power Query Open ID and OAuth 2.0 Rest API
- 7 years ago
let token_url = "tokenurl.com", api_base_url = "apiurl.com", qry_str = "?myparameter", body="grant_type=password&client_id=CLIENTID&client_secret=PASSWORD&username=EMAIL&password=PASSWORD", Source = Json.Document(Web.Contents(token_url, [ Headers = [#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(body) ] ) ), token = Source[access_token], data= Json.Document(Web.Contents(api_base_url&qry_str, [ Headers = [ #"Authorization"="Bearer "&token,#"Content-Type"="application/json"] ] ) ), in dataI finally got it working with this code. Thanks for the support.
Thanks for the reply. The client_id and client_secret go to the body, not the header. I was able to get the first part working. I am able to get the access token from the first post request. With the get request, I am now having problems getting authentic. Please see my M code below:
let
token_url = "token_url.com",
api_base_url = "api_base_url.com",
qry_str = "get_this_data",
body="grant_type=password&client_id=THIS_IS&client_secret=Y&[email protected]&password=PASSWORD",
Source = Json.Document(Web.Contents(token_url,
[
Headers = [#"Content-Type"="application/x-www-form-urlencoded"],
Content=Text.ToBinary(body)
]
)
),
token = Source[access_token],
data= Json.Document(Web.Contents(api_base_url&qry_str,
[
Headers = [
#"Authorization"="Bearer "&token,#"Content-Type"="application/json",#"Keep-Alive"="1"]
]
)
)
in
dataIn this code, Source is returning the token successfully. But when I pass it on to the next one, I get an authentication error for the result of data. It says access is forbidden. (It works on Python). Maybe I am doing the second part wrong? I don't think keep_alive is necessary.
Can you provide a screenshot of the error you're receiving? And can you remove the Keep-Alive parameter?
Here are a few things to check:
- What's the data type of the token variable? Is it a text value?
- Is the Url for the endpoint constructed correctly? Generally, REST-ful APIs require query parameters to be appended to the endpoint via a question mark ('?'), like api_base_url.com?get_this_data. I don't see a question mark anywhere in your Url.
- What data format does the endpoint expect? I suggest adding Accept = "application/json" to the 'Headers' record.
- numersoz7 years agoHelper III
let token_url = "tokenurl.com", api_base_url = "apiurl.com", qry_str = "?myparameter", body="grant_type=password&client_id=CLIENTID&client_secret=PASSWORD&username=EMAIL&password=PASSWORD", Source = Json.Document(Web.Contents(token_url, [ Headers = [#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(body) ] ) ), token = Source[access_token], data= Json.Document(Web.Contents(api_base_url&qry_str, [ Headers = [ #"Authorization"="Bearer "&token,#"Content-Type"="application/json"] ] ) ), in dataI finally got it working with this code. Thanks for the support.