Forum Discussion
Issue with getting data via API with bearer token
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
Hi
i had the same issue which i was able to figure it with some tweak.
Like a post request with a get with bearer token to get the data
hope this helps
let
url = "http://###########/Portal/api/v1/Token",
url1 = "http://#########/Portal/api/v1/myapi?FromDate=2017/09/29 01:00:00&ToDate=2017/09/29 23:00:00&",
GetJson = Web.Contents(url,
[
Headers = [#"Accept"="application/json",
#"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"],
Content = Text.ToBinary("grant_type=password&username=###########&password=###########&client_id=###########&client_Secret=###########")
]
),
FormatAsJson = Json.Document(GetJson),
AccessToken = FormatAsJson[access_token],
AccessTokenHeader = "bearer " & AccessToken,
GetJson1 = Json.Document(Web.Contents(url1 , [Headers=[Authorization= AccessTokenHeader ]]))
in
GetJson1