Forum Discussion
Issue with getting data via API with bearer token
Hi Anonymous,
I think your formula based on below article, right?
Get Data from Twitter API with Power Query
Web.Contents method not support directly add username and password to content query.
Usually logic of request:
1. Call login API to get the access token.
2. Use access token call other operation api.
Sample:
WebResponse = Web.Contents(WebServiceURI,
[Content = Text.ToBinary(WebServiceContent),
Headers = [Authorization="Bearer " & AccessToken,
#"Content-Type"="application/json",
Accept="application/json"],
Timeout = WebTimeout])
In addition, API can allow "username", "password", "token" as the parameters to query string, but they must be defined in design.
For example:
RequestUri= BasUri?UserName:xxxxx&Passowrd:xxxxxx&Token:xxxxx
I'd recommend you take a look at the Official documentation and find out the correct way to call api.
Regards,
Xiaoxin Sheng
Thank you Xiaoxin Sheng ( Anonymous ),
The logic you have provided is what I am trying to perform as the token has a 30 min lifespan and I can't get users of the report to manually enter a token each time.
To call a token I have to send a POST request with the username, password and grant_type embedded as content. The API does not accept a URI with parameters to return a token.
This works to return my token:
GetJson = Web.Contents(url,
[
Headers = [#"Accept"="application/json",
#"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"],
Content = Text.ToBinary("username=uXXXXXX&password=pXXXXXX&grant_type=password")
]
),
FormatAsJson = Json.Document(GetJson),
// Gets token from the Json response
AccessToken = FormatAsJson[access_token],
AccessTokenHeader = "bearer " & AccessTokenThis will give me a variable (AccessTokenHeader) with the access token. If there is another way to do this please let me know.
- Anonymous9 years agoNot applicable
Hi Anonymous,
Can you share some detail content of the api which you want to invoke?
>>The logic you have provided is what I am trying to perform as the token has a 30 min lifespan and I can't get users of the report to manually enter a token each time.
You can write a custom function to get token which your account, then invoke this method before operation other api.
Regards,
Xiaoxin sheng
- Anonymous9 years agoNot applicable
Hi Anonymous,
Thanks for following this through. The instructions provided for the API are as follows:
1. Create an OAuth2 Session
Create a session and get a token (that you need to pass in your Web API request) using your user credentials by doing a “HTTP POST“ request on the URL.The base URL used for all operations is formatted as follows: https://{deployedAPIServer}/api/{resource}
The “https://{deployedAPIServer}/token/" Microsoft OWIN oauth token endpoint with parameters:
"username", with value: the user login
"password", with value: the user password
"grant_type" : password (Resource Owner Password Credentials)
The content-type of the request should be: application/x-www-form-url encoded
Link to image of C# snippet provided in instructions
If the credentials are valid, the response would be a JSON object:
{"access_token":"..base64 encoded string","token_type":"bearer","expires_in":seconds}"
2. Call a Secure API
Call a secure rest API with token.
Set the returned token in your request header as a "Bearer" authentication and make the post, get, delete, .. HTTP call.Link to image of C# snippet provided in instructions
Does this help?
- Anonymous9 years agoNot applicable
Hi Anonymous,
Sorry for slow response.
I haven't found a way to use power query invoke the sign in credentials, perhaps you can refer to below steps:
1. Package the sample code to a webservice.
Sample : (just for reference)
Public string GetToke(string UserName, string Password,string deployedAPIServer) { string token=""; using (HttpClient client= new HttpClient()) { Client.DefaultRequestHeaders.Add("Accept","application/json"); FormUrlEncodedConten credentialsContent =new FormUrlEncodedConten(new Dictionary<string,string> {{ "username",UserName},{"password",Password},{"scope","read"},{"grant_type","password"}}; Task<HttpResponseMesage> requestTask= client.PostAsync("https://{"+deployedAPIServer+"}/api/token",credentialsContent); string jwt= requestTask.Result.Conten.ReadAsStringAsync().Result; token= (string)jObject["access_token"]; }; return token; }
2. At query editor side, use web.content to invoke above service and store the access token.Sample:
let username="xxxx", passowrd="xxxxx", apiurl="xxx.xxx.xxx", AccessToken= Web.Contents(WebServiceURL& "?GetToke("+username+&","&+passowrd+&","&+apiurl+&")") in AccessToken
3. Use token to operation other functions.Some useful links:
Understanding the Username-Password OAuth Authentication Flow
Regards,
Xiaoxin sheng