I am completely new to APIs and have done some reading. After not being able to connect to the API via a Web data source (https://api.channeladvisor.com/oauth2/token), I am trying to connect to Channel Advisor's API via Power Query (Excel, but it doesn't work in Power BI, either). I am getting errors when I try to call the custom function I made from another query.
I found this post online, and I was able to use some of the code snippets posted as follows.
STEP 1: I created a custom function called "GetAccessToken"
() =>
let
url = "https://api.channeladvisor.com/oauth2/token",
headers = [#"Authorization"="Basic xxxxxx"],
postBody = [
grant_type = "refresh_token",
refresh_token = "xxxxxx"
],
response = Json.Document(Web.Contents(url,
[
Headers = headers,
Content = Text.ToBinary(Uri.BuildQueryString(postBody))
])),
access_token = response[access_token]
in
access_token
That part works.
STEP 2: I created a query calling the custom function to access Channel Advisor
Then I created another query that is supposed to forge the connection, but either I get a 400 error, or I get a simple table that says "error | invalid client":
let
Source = Json.Document(Web.Contents("https://api.channeladvisor.com/v1/orders",
[Headers=[#"Authorization"="bearer " & GetAccessToken(),
#"accept" = "text/plain",
#"Content-Type"="application/json"],
ManualStatusHandling = {404, 400}]))
in
Source
I have not been able to find any comprehensive instructions anywhere on how to fix this, or what I need to do to get the result that I am looking for.
Update: I was able to successfully connect to the API in Postman. When the initial token expires, the token that "GetAccessToken" procures when run as a straight query works fine. I redid the permissions in Power Query for the umpteenth time, and now all of a sudden it returns data. I'll keep poking and report back.