简体   繁体   中英

Using Titanium with Windows Azure

I want to integrate Windows Azure Services within the app created in Titanium, same as we use to do with eclipse for Android. I am newbie working with Titanium. Please help me providing some useful links to get started with.

This is how i got the data using REST API.

function getWebserviceURL() {

    return "https://database_name.azure-mobile.net/tables/";
}

function getCountryMasterData() {

    var xhr = Titanium.Network.createHTTPClient();
    xhr.onerror = function(e)
    {
        alert(e.toString);
    };
    xhr.onload = function() {
        alert(this.responseText);
    };
    var request_url = getWebserviceURL() + "table_name";
    xhr.open('GET', request_url);
    xhr.setRequestHeader("Accept","application/json");
    xhr.setRequestHeader("X-ZUMO-APPLICATION", "APP KEY");
    xhr.setRequestHeader("Host", "database_name.azure-mobile.net");
    xhr.send();
}

Edit:

I wrote a Titanium SDK for Windows Azure Mobile Services. Check it out here: https://github.com/rfaisal/tiny-azuresdk-titanium

Original Answer:

First login to facebook to get the access_token (let's call it authorization_grant):

var fb=require('facebook');
fb.appid = 000000000000000; //replace this with your facebook app id
fb.permissions = ['email']; // add or remove permissions
fb.addEventListener('login', function(e) {
    if (e.success) {
        Ti.API.info('authorization_grant: '+fb.getAccessToken()); // prints to console, save for future use
    }
});
fb.authorize();

Then, get access_token (not facebook, but azure) from authorization_grant:

var authorization_grant = 'CAAHn34BO0R0BABtJyxdrZCQzakdmgJZBoQa0U...'; //saved from previous step
var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(30000);
xhr.onload=function() {
    Ti.API.info('access_token: '+JSON.parse(this.responseText).authenticationToken); // save this for requesting protected resource
};
xhr.onerror= function() {
    Ti.API.info('Auth Failure response: '+this.responseText);
};
xhr.open('POST', 'https://my_resource_auth_server.azure-mobile.net/login/facebook');
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "application/json");
xhr.send({
    "access_token" : authorization_grant
});

Finally, get the protected resource by access_token:

var access_token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsI...'; //saved from previous step
var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(30000);
xhr.onload=function() {
    Ti.API.info('Protected Resource: '+this.responseText);
};
xhr.onerror= function() {
    Ti.API.info('Error response: '+this.responseText);
};
xhr.open('GET', 'https://my_resource_auth_server.azure-mobile.net/api/some_resource');
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("X-ZUMO-AUTH", access_token); // this is the magic line
xhr.send();

To get resource that require application level access, add a header called X-ZUMO-APPLICATION and pass your Application Key obtained from Azure website. Similarly, to access resource that requires admin level access, pass your Mobile Service master key obtained from Azure website as X-ZUMO-MASTER header.

The oAuth flow being followed here: 在此输入图像描述

The originally publish article: https://rfaisalblog.wordpress.com/2014/03/01/oauth-flow-for-mobile-apps-with-an-external-identity-server/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM