Log in Drupal site in Phonegap
I am trying to allow my users to log in from their mobile app to a Drupal site in order to retrieve some of their profile information. Anybody knows how to do it? I have the Services module on the site which is already s开发者_C百科erving some JSON files for the news.
With the Services 3.x module for Drupal 6 or 7, you need to create an endpoint through the UI, e.g.:
- Name: my_services
- Endpoint title: My Services
- Server: REST
- Path to endpoint: my_services_path
Then enable the following resource for the newly created endpoint:
- User -> Login
If your mobile app is HTML5 and JS based then you can use Ajax, for example in PhoneGap with jQueryMobile you could make a login page like this that would load your custom JS file:
<div data-role="page" id="page_login">
  <script type="text/javascript" charset="utf-8" src="my_app_login.js"></script>
  <div data-role="header">
    <h1>Login</h1>
  </div><!-- /header -->
  <div data-role="content" class='content'>
    <div>
      <label for="page_login_name">Username</label>
      <input type="text" id="page_login_name" />
    </div>
    <div>
      <label for="page_login_pass">Password</label>
      <input type="password" id="page_login_pass" />
    </div>
    <fieldset>
      <div><button type="button" data-theme="b" id="page_login_submit">Login</button></div>
    </fieldset>
  </div><!-- /content -->
</div><!-- /page -->
Then when your app's login button is clicked, this implementation would go in my_app_login.js with your app's code:
$('#page_login_submit').on('click',function(){
  // grab form input
  var name = $('#page_login_name').val();
  if (!name) { alert('Please enter your user name.'); return false; }
  var pass = $('#page_login_pass').val();
  if (!pass) { alert('Please enter your password.'); return false; }
  // make the services call
  var data_string = 'username=' + encodeURIComponent(name);
  data_string += '&password=' + encodeURIComponent(pass);
  $.ajax({
      url: "http://example.com/?q=my_services_path/user/login.json",
      type: 'post',
      data: data_string,
      dataType: 'json',
      error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert('page_login_submit - failed to login');
        console.log(JSON.stringify(XMLHttpRequest));
        console.log(JSON.stringify(textStatus));
        console.log(JSON.stringify(errorThrown));
      },
      success: function (data) {
        alert('You are now logged in!');
      }
  });
  return false;
});
Regardless of what platform(s) you are using to develop your mobile application, the call to the Services user login resource will be the same.
I have a solid PhoneGap and Drupal example here:
http://tylerfrankenstein.com/code/android-app-with-drupal-7-services-phonegap-and-jquery-mobile
Alternatively, I made a module that provides a solid framework for communicating with a Drupal 6 or 7 site via PhoneGap and Services 3.x, it is called DrupalGap:
- http://drupal.org/project/drupalgap
- https://github.com/signalpoint/DrupalGap
- https://market.android.com/details?id=com.drupalgap
- http://tylerfrankenstein.com/drupalgap
There's a Drupal Phonegap module that allows an Android app to login, post and view articles on your Drupal site. I would recommend that you look at the source code for the module and the Android app as a reference for your app.
Module: http://drupal.org/project/phonegap
Source for Android app: http://code.google.com/p/drupal-phonegap/
There's also an example on how to do this here: http://www.tylerfrankenstein.com/user/4/code/drupal-json-services-examples-phonegap-jquery-mobile-android
And if you're developing an iPhone app check this post out: http://www.jefflinwood.com/2011/10/easier-drupal-plugin-for-phonegap-ios-installation/
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论