Forum Discussion
Issue with getting data via API with bearer token
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?
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