Blogs / Thiago Campos Viana / REST API, Basic HTTP Auth, and PhoneGap using jQuery

REST API, Basic HTTP Auth, and PhoneGap using jQuery

Saturday 18 June 2011 12:47:07 pm

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

By : Thiago Campos Viana

Today I'll show how to use REST API with basic http auth using jquery.

1. Change your rest.ini settings:

[Authentication]
RequireAuthentication=enabled
#AuthenticationStyle=ezpRestOauthAuthenticationStyle
AuthenticationStyle=ezpRestBasicAuthStyle

2. Local host test:

$.ajax({
url: 'http://localhost/ez_site/api/ezp/content/node/2',
otherSettings: 'othervalues',
username: 'your_user_name',
password: 'your_user_pass',
complete: function(data) {
    //code goes here
}
});

3. Android using jquery mobile and phonegap test (Using the code above didn't work, so I used the code bellow):

    var username = 'your_user_name';
    var pass = 'your_user_pass';
    //10.0.2.2:80 is the localhost in android emulator, app needs internet access
    $.get("http://"+username+":"+pass+"@10.0.2.2:80/ez_site/api/ezp/content/node/2",
               function(data) {
                 //code goes here

   });

Note: If RequireAuthentication is disabled you can just go this way:

$.ajax({
url: 'http://localhost/ez_site/api/ezp/content/node/2',
complete: function(data) {
    //code goes here
}
});

PhoneGap:

    //10.0.2.2:80 is the localhost in android emulator, app needs internet access
    $.get("http://10.0.2.2:80/ez_site/api/ezp/content/node/2",
               function(data) {
                 //code goes here

   });

I don't know if there's a way to use phonegap and oAuth.