Created
March 1, 2016 22:51
-
-
Save khle/dd8f9e3bfce9543ebc41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var rp = require('request-promise'), | |
| Rx = require('rx') | |
| const uriAuthToken = 'https://login.windows.net/[YOUR_TENANT].onmicrosoft.com/oauth2/token' | |
| const uriResource = 'http://[YOUR_RESTRICTED_RESOURCE]' | |
| const optionsAuthToken = { | |
| method: 'POST', | |
| uri: uriAuthToken, | |
| form: { | |
| grant_type: 'password', | |
| scope: 'openid', | |
| resource: 'http://samplewebservice.azure-mobile.net', //If you follow my example | |
| client_id: 'client-id-of-the-AAD-native-app', | |
| username, | |
| password | |
| }, | |
| json: true | |
| } | |
| const observable = Rx.Observable.fromPromise(rp(optionsAuthToken)) | |
| .map(response => { | |
| const optionsResource = { | |
| uri: uriResource, | |
| headers: { | |
| authorization: response.token_type + ' ' + response.access_token | |
| }, | |
| json: true | |
| } | |
| return Rx.Observable.fromPromise(rp(optionsResource)) | |
| }) | |
| .flatMap(item => item) | |
| observable.subscribe(response => { | |
| console.log(response) | |
| }, error => { | |
| console.log("an error occurred", error, error.message, error.stack) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment