英文:
Connecting to Channel Advisor API
问题
"我对API完全不了解,并已经进行了一些阅读。在无法通过Web数据源(https://api.channeladvisor.com/oauth2/token)连接到API后,我试图通过Power Query(Excel,但在Power BI中也不起作用)连接到Channel Advisor的API。当我尝试从另一个查询中调用我创建的自定义函数时,出现错误。
我在这个帖子上找到了一些代码片段,并能够使用其中的一些代码片段如下。
步骤1:我创建了一个名为“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
那部分有效。
步骤2:我创建了一个调用自定义函数以访问Channel Advisor的查询
然后,我创建了另一个查询,应该建立连接,但要么出现400错误,要么得到一个简单的表格,上面写着“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
我还没有找到任何关于如何解决这个问题,或者我需要做什么才能获得我要的结果的综合说明。
更新:我能够成功地在Postman中连接到API。当初始令牌过期时,作为直接查询运行时“GetAccessToken”获取的令牌能够正常工作。我重新为Power Query重复了权限,现在突然间返回数据。我会继续尝试并回报结果。"
英文:
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.
答案1
得分: 0
以下是已翻译的内容:
自从我的初始问题是我找到了一些碎片信息,但没有完整的答案,我决定在这里发布解决方案。我希望这是允许的。
我的目标是连接到Channel Advisor的订单。
步骤1:创建自定义函数
先决条件: 在Channel Advisor的开发者控制台中创建一个应用程序,并记录访问令牌和刷新令牌。
建议: 在创建函数之前先测试代码(省略() =>
并查看它是否有效,然后添加它并命名它。在我的情况下,函数被称为GetAccessToken
。)
这是M代码:
() =>
let
url = "https://api.channeladvisor.com/oauth2/token",
headers = [# "Authorization" = "Basic XXXXX"],
postBody = [
grant_type = "refresh_token",
refresh_token = "XXXXX"
],
response = Json.Document(Web.Contents(url,
[
Headers = headers,
Content = Text.ToBinary(Uri.BuildQueryString(postBody))
])),
access_token = response[access_token]
in
access_token
这获取基于刷新令牌的当前访问令牌(在Channel Advisor中称为Bearer令牌)。
步骤2:创建连接查询
这是初始查询:
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
这是该查询的输出:
单击表格中的“List”单词,位于“value”单词旁边。这会在查询中插入一个步骤“导航”。它还会在顶部打开一个名为“List Tools”的选项卡。在“List Tools”选项卡中的“数据 | 转为表格”上单击:
它将呈现一个对话框。我只是点击了“确定”:
最后一步是展开结果列:
取消选中“使用原始列名称作为前缀”并单击“确定”。哇,这就是您的数据。
需要注意:
确保两个数据源都设置为匿名连接。否则,您将收到错误消息。如果遇到问题,首先在Postman中尝试。
这里的屏幕截图是屏幕截图的截图,所以如果它们有点模糊,这是原因。我没有时间/精力重新拍摄它们。
英文:
Since my initial issue was that I found bits and pieces to this, but no complete answer, I have decided to post the solution here. I hope this is allowed.
My goal was to connect to Channel Advisor orders.
STEP 1: CREATE THE CUSTOM FUNCTION
Prerequisite: create an application in Channel Advisor's developer console and record the access token and refresh token.
Recommended: test the code first before creating the function (leave out the () =>
and see if it works, then add it in and name it. In my case, the function is called GetAccessToken
.)
This is the M code:
() =>
let
url = "https://api.channeladvisor.com/oauth2/token",
headers = [#"Authorization"="Basic XXXXX"],
postBody = [
grant_type = "refresh_token",
refresh_token = "XXXXX"
],
response = Json.Document(Web.Contents(url,
[
Headers = headers,
Content = Text.ToBinary(Uri.BuildQueryString(postBody))
])),
access_token = response[access_token]
in
access_token
This gets the current access token (= bearer token in Channel Advisor "speak") based on the refresh token.
STEP 2: CREATE THE CONNECTION QUERY
This is the initial query:
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
And this is the output from that query:
Click on the word "List" in the table, next to the word "value". This inserts a step "Navigation" into the query. It also opens up the list an a tab called "List Tools" at the top. Click on "Convert | To Table" on the ribbon in the "List Tools" tab:
It will present you with a dialog box. I simply clicked "OK":
The last step is to expand the resulting column:
Uncheck "Use original column name as prefix" and click "OK". Voila, here is your data.
OF NOTE:
Make sure that both data sources are set to connect anonymously. Otherwise, you will get an error. Also, try things in Postman first if you run into trouble.
The screenshots here are screenshots of screenshots, so if they're a bit fuzzy, that is the reason. I didn't have the time/energy to retake them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论