Adding Facebook login to ionic app

2019-04-10 20:03发布

问题:

I am newbie to a Angular and Ionic as well.

Tried the following tutorial:

https://www.sitepoint.com/how-to-integrate-facebook-login-into-a-cordova-based-app/

Steps performed: 1.In app browser is installed.

  1. Included libraries.

    <script src="lib/ngCordova/dist/ng-cordova.js"></script>
    <script src="lib/ng-cordova-oauth/dist/ng-cordova-oauth.js"></script>
    <script src="cordova.js"></script>
    
  2. Include code inside index.html before body closing tag:

    <script>
       window.fbAsyncInit = function() {
        FB.init({
        appId      : 'MyappID',
        xfbml      : true,
        version    : 'v2.6'
      });
    };
    
      (function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/en_US/sdk.js";
         fjs.parentNode.insertBefore(js, fjs);
         }(document, 'script', 'facebook-jssdk'));
    </script>
    
  3. Following is my login.html :

     <button class="btn-margin-left button button-positive" ng-click="menuCtrl.fbLogin()">
      <i class="ion-social-facebook icon-size1 "></i>
     </button>
    
  4. This is my controller :

    menuCtrl.fbLogin = function ($cordovaOauth,$http) {
        facebookLogin(window.cordovaOauth, window.http);
    }
    
  5. This is my app.js :

    angular.module("cgsi",['ionic', 'ngCordova','ngCordovaOauth'])
      .config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider, $httpProvider, $logProvider, $sceDelegateProvider,$cordovaOauth,$http) {
      window.cordovaOauth = $cordovaOauth;
      window.http = $http;
     });
    

I am getting error Unknown provider $cordovaOauth Please let me know where i am going wrong?

回答1:

first of all you need to have account as facebook developer and generate one app for test,

https://developers.facebook.com/

then create one cordova application and and set one button in UI side.

Then you need to install following plugin in your test-app.

https://github.com/Wizcorp/phonegap-facebook-plugin

or

https://github.com/jeduan/cordova-plugin-facebook4

please go through above plugin carefully because you need to add your app_id and app_name which you can find from developer-facebook account where you created your test app.

and then you need to go through related plugin documentation for getting facebook login code.

Thank you.



回答2:

I can't see your controller has dependency injected or not, I hope you will get what my mean.

if still problem is same. use Adding google plus login to ionic app

And instead of google try facebook. after completion of installation you can add all other plugins that you want.