0
votes

I built an Cordova App for ios/android. The app uses an Azure mobile service tied to Azure Active Directory. This works well. When I try to do a directory search, I am prompted by Azure to authenticate using my tenant credentials.

Next steps:

We have few apis which has some C.R.U.D on an Azure SQL database. The API works fine and I get data from it (while unsecured) by navigating to https://mat.azurewebsites.net/api/values.

Once the user has authenticated using the ADAL Cordova library (working just fine and I receive a token back), I would like to execute a GET request to my Web API and return data.

This is where I'm having trouble and not sure how to construct the request to the API so it

We have similar question posted here How do I query Azure Web API from Cordova Azure Authenticated app

But no answer there.

I need to call multiple apis.... In AngularJs adal we can pass multiple endpoints but here I don't know that how we can pass.

var authority = "https://login.microsoftonline.com/TenantId",

redirectUri = "https://Mobile",

resourceUri = "https://graph.microsoft.com" // I am not sure what should be here,

clientId = "xxxxxxxxxxxxxxx";


var url = "https://xxxxx-api.cloudapp.net/v1/purchaseorders?
$orderby=OrderPlacementDate desc"

req.open("GET", url, true);
    req.setRequestHeader('Authorization', 'Bearer ' + 
 authResult.accessToken);
    req.setRequestHeader('XXX.FunctionGroup', 'PurchaseProduct');

    req.onload = function (e) {
        if (e.target.status >= 200 && e.target.status < 300) {
            app.error('Valid');

            app.renderData(JSON.parse(e.target.response));
            return;
        }
        app.error('Data request failed error: ' + e.target.response + '.......' + e.target.status);
    };
    req.onerror = function (e) {
        app.error('Data request failed onerror: ' + e);
    }

    req.send();

@@@@@@ this is the code for authenticate

   authenticate: function (authCompletedCallback) {

    app.context = new Microsoft.ADAL.AuthenticationContext(authority);
    app.context.tokenCache.readItems().then(function (items) {


        if (items.length > 0) {
            authority = items[0].authority;
            app.context = new Microsoft.ADAL.AuthenticationContext(authority);
        }
        // Attempt to authorize user silently
        app.context.acquireTokenSilentAsync(resourceUri, clientId)
            .then(authCompletedCallback, function () {
                // We require user cridentials so triggers authentication dialog
                app.context.acquireTokenAsync(resourceUri, clientId, redirectUri)
                    .then(authCompletedCallback, function (err) {
                        app.error("Failed to authenticate: " + err);
                    });
            });
    });

}
1
You need to pass the token in your api call.techwestcoastsfosea
Do you have any example? I am adding little bit code in the question description.core developer
try setting var mytoken = "token you get" then apiurl/endpoint?token=mytoken and whatever else needs to be done. This will validate if you are getting a validate token. I am suspecting when you assign bearer that is where it is failing somehow.techwestcoastsfosea
What about the resourceUri..? it will be still the same 'graph.microsoft.com'?core developer
@bankyogi-mobile-app I tried by adding a token in querystring but I am getting 404.... Even though If I do not pass token then still I am getting 404... However the same api is working fine in a other project.core developer

1 Answers

-1
votes

You need to pass your token in the HTTP authorization header as a Bearer token. In the format "Bearer TOKEN"

Test the http call out in Postman make a HTTP GET request copy the token from your app into the authorization header with "Bearer " in front of the token.

// Makes an API call to receive the user list
requestData: function (authResult, searchText) {
    var req = new XMLHttpRequest();
    var url = resourceUri + "/" + authResult.tenantId + 
"/users?api-version=" + graphApiVersion;
    url = searchText ? url + "&$filter=mailNickname eq '" + 
searchText + "'" : url + "&$top=10";

    req.open("GET", url, true);
    req.setRequestHeader('Authorization', 'Bearer ' + 
authResult.accessToken);

    req.onload = function(e) {
        if (e.target.status >= 200 && e.target.status < 300) {
            app.renderData(JSON.parse(e.target.response));
            return;
        }
        app.error('Data request failed: ' + e.target.response);
    };
    req.onerror = function(e) {
        app.error('Data request failed: ' + e.error);
    }

    req.send();
},

https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-devquickstarts-cordova