Forum Discussion
Channel Advisor API Connection with auth key creation
Afternoon All!
Gonna be honest i'm racking my **bleep** brains out with this one but i'm getting no where.
I tested a rest function in Postman and all works brilliant returning the data i need. unfortunately i want to build this into a power query now.
to generate a Oauth2 key i need to send a refresh key with username and password in order to get a return , then use the vaule it returns to request a report.
I'm hoping some wise individual will have at least some idea how to pull this off as i cannot i've tried using the web API fuction built into PQ but its just doesn't allow me to do anything. any ideas?
Thanks in advance 🙂
Great! Cheers!
The response is in Json format, so you just need to add a step at the end of the custom function to extract access_token value. Don't forget to add a comma at the end of previous response step.
..................... response = Json.Document(Web.Contents(.......................)), access_token = response[access_token] in access_tokenJing
29 Replies
- AnonymousNot applicable
I am trying to use everybody's code snippets to connect to Channel Advisor's API. This part works:
() => let url = "https://api.channeladvisor.com/oauth2/token", headers = [#"Authorization"="Basic xxxxxxx="], postBody = [ grant_type = "refresh_token", refresh_token = "xxxxxxxx" ], response = Json.Document(Web.Contents(url, [ Headers = headers, Content = Text.ToBinary(Uri.BuildQueryString(postBody)) ])), access_token = response[access_token] in access_tokenWhat doesn't work is this part:
let Source = Json.Document(Web.Contents("https://api.channeladvisor.com/oauth2/token", [Headers=[#"Authorization"="bearer " & GetAccessToken(), #"accept" = "text/plain", #"Content-Type"="application/json"], ManualStatusHandling = {404, 400}])) in SourceI have tried it with "Bearer" vs. "bearer" and a few other things, but either I get a 400 error OR the Power Query output simply gives me a small table that says "error | invalid client".
What am I doing wrong?
- SomeDataDudeAdvocate I
hi Anonymous ,
Could you try to replace your last query with:
let Source = Json.Document(Web.Contents("search_url", [Headers=[#"Authorization"= GetAccessToken(), #"accept" = "text/plain", #"Content-Type"="application/json"], ManualStatusHandling = {404, 400}])) in Source- AnonymousNot applicable
Thank you for responding. If I make that change, I get this error:
DataSource.Error: The remote name could not be resolved: 'search_url' Details: search_url
- lbendlinSuper User
Were you able to accomplish the first part in Power Query (ie getting the refresh key) ?
- TrysHisBestNew Member
Unfortuntely not I've been trying a couple variations of Jing's code above, but unfortunately not getting far. i'm sure its me but unfamilar with post requests so sharp learning curve at the moment 😄
- v-jingzhangCommunity Support
Hi TrysHisBest
Do you send a Post request to get the key? If so, you need to put request body in Content parameter of Web.Contents function.
Here is an example for Post request. Create a blank query, open its Advanced Editor, remove the code there and paste below code to it.
let url = "https://xxxxxxxxxxxxxxxxxxxxxxxx", headers = [#"Content-Type" = "application/x-www-form-urlencoded", #"Accept" = "*/*"], postBody = [ grant_type = "xxxxxxxxxx", username = "xxxxxxxxxxxxxxxx", password = "xxxxxxxxxxx" ], response = Json.Document(Web.Contents(url, [ Headers = headers, Content = Text.ToBinary(Uri.BuildQueryString(postBody)) ])) in responseOnce you get the key, you can then use it in another Web.Contents statement to get data for your report.
Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.- TrysHisBestNew Member
Hi Jing,
I've tried this and had this come back as the first response?
DataSource.Error: Web.Contents failed to get contents from 'https://api.channeladvisor.com/oauth2/token' (400): Bad Request
Details:
DataSourceKind=Web
DataSourcePath=https://api.channeladvisor.com/oauth2/token
Url=https://api.channeladvisor.com/oauth2/token
After that i tried ammending to add in the refresh token but met with the same response above?let url = "https://api.channeladvisor.com/oauth2/token", headers = [#"Content-Type" = "application/x-www-form-urlencoded", #"Accept" = "*/*"], postBody = [ grant_type = refresh_token, refresh_token ="xxxxx" username = "xxxxxxxxxxxxxxxx", password = "xxxxxxxxxxx" ], response = Json.Document(Web.Contents(url, [ Headers = headers, Content = Text.ToBinary(Uri.BuildQueryString(postBody)) ])) in response- v-jingzhangCommunity Support
Hi TrysHisBest
You need to modify the headers and body content according to your POST request. Can you provide a screenshot of the correct request in Postman (remove sensitive info)?
I think grant_type value should also be wrapped by double quotes.
grant_type = "refresh_token",Jing