Forum Discussion
Channel Advisor API Connection with auth key creation
- 4 years ago
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
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
Hi v-jingzhang ,
Really do apprecaite the help.
This is what i made in postman and getting the auth key from. works no problem
Then have a seperate tab to use a GET request to get the data i need from a different url with the Auth02 key.
Again thanks very much 🙂
- v-jingzhang4 years ago
Community Support
Hi TrysHisBest
Your username and password are passed in Authorization Header with Basic Auth type, not in Body, so they should be in Headers parameter rather than Content parameter of Web.Contents().
I did some google searching for passing basic Authorization username and password, and found a blog and a thread:
Using Power Query to get data from an API that uses a Username and Password combination
Passing username and password in Basic authentication (microsoft.com)
Both suggest to put username and password in username:pasword format, then encode the string with base64 encoding. Then put the encoded string into Headers. For encoding use the link https://www.base64encode.org/
Based on above, you could try below code
let url = "https://api.channeladvisor.com/oauth2/token", headers = [#"Authorization"="Basic xxxxxxxxxxxxx"], postBody = [ grant_type = "refresh_token", refresh_token = "xxxxxxxxxxxxxxxx" ], response = Json.Document(Web.Contents(url, [ Headers = headers, Content = Text.ToBinary(Uri.BuildQueryString(postBody)) ])) in responseIf you can get correct token by using above code, then you can transform the query to a custom function so that it can be called by other queries. Just add () => before let:
() => let url = "https://api.channeladvisor.com/oauth2/token", headers = [#"Authorization"="Basic xxxxxxxxxxxxx"], postBody = [ grant_type = "refresh_token", refresh_token = "xxxxxxxxxxxxxxxx" ], response = Json.Document(Web.Contents(url, [ Headers = headers, Content = Text.ToBinary(Uri.BuildQueryString(postBody)) ])) in responseThen create another query for the GET request which calls the function. Assume the earlier custom function query is called GetAccessToken.
let Source = Json.Document(Web.Contents("https://xxxxxxxxxxxxxxxxxxxx", [Headers=[#"Authorization"="Bearer "&GetAccessToken()]])) in SourceHope this helps.
Jing
- TrysHisBest4 years agoNew Member
v-jingzhang You are brilliant!
I've managed to get the Access token and invoke it as a custom response! brilliant!
Its returned 3 values
I Just need the access_token part of the response could i reference just that that in the GET request? or do i need to alter the custom function?
Its seems to get confused when all 3 parts are still in the Custom Function
Thanks very much for all your help getting me this far!
- v-jingzhang4 years ago
Community Support
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
- SomeDataDude3 years ago
Advocate I
Hi v-jingzhang,
Thanks for sharing the solution. Could you help me with the following problem?
I created the function, that is working fine. But if I put the function in the second query, it doesn’t work. When I put the token directly in the query it works fine. I use the following M code:
let Source = Json.Document(Web.Contents("URL", [Headers=[#"Authorization"="Bearer "&GetAccessToken(), #"accept" = "text/plain", #"Content-Type"="application/json"], ManualStatusHandling = {404, 400}])) in SourceMy custom query is called GetAccessToken.
What am I doing wrong?
This is the error I get: Formula.Firewall: Query 'Query5' (step 'Source') is accessing data sources that have privacy levels which cannot be used together. Please rebuild this data combination.
- lbendlin3 years ago
Super User
As it says. Modify the query privacy settings so that they match. Or Inline the token query.
- Anonymous3 years agoNot applicable
Hi Jing,
Thank you for providing the solution. After trying your code, I got following error "DataSource.Error: Web.Contents with the Content option is only supported when connecting anonymously.
Details:
DataSourceKind=Web
DataSourcePath=https://api-eu.ariba.com/v2/oauth/token"Could you please help us out here?