添加google plus登录到离子应用程序

时间:2016-05-30 10:10:55

标签: angularjs ionic-framework cordova-plugins

我正在尝试将google plus登录添加到我的ionic app。 点击此链接会给我一个错误。

https://ionicthemes.com/tutorials/about/google-plus-login-with-ionic-framework

错误是:cannot read property googleplus of undefined.

这是我的app.js

.run(function($ionicPlatform) {
   $ionicPlatform.ready(function() {

    if (window.cordova && window.cordova.plugins.Keyboard) {
    cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
  cordova.plugins.Keyboard.disableScroll(true);

}
if (window.StatusBar) {
  // org.apache.cordova.statusbar required
  StatusBar.styleDefault();
}
});
})

3 个答案:

答案 0 :(得分:3)

在Device(android)中配置身份验证的步骤

  1. ionic start newApp
  2. ionic platform add android
  3. cordova plugin add cordova-plugin-inappbrowser
  4. bower install ngCordova
  5. bower install ng-cordova-oauth -S
  6. 将两个脚本都包含在index.html

    之上cordova.js
    <script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
    <script src="lib/ng-cordova-oauth/dist/ng-cordova-oauth.js"></script>
    <script src="cordova.js"></script>
    
  7. 依赖注入

  8. 包含以下代码

    $scope.googleLogin = function() {
    console.log('In My Method');
    $cordovaOauth.google("Client ID", ["https://www.googleapis.com/auth/urlshortener", "https://www.googleapis.com/auth/userinfo.email"]).then(function(result) {
        console.log(JSON.stringify(result));
        // results
    }, function(error) {
        // error
        console.log('In Error');
        console.log(error);
    });
    }
    
  9. 添加按钮以查看文件并调用函数

答案 1 :(得分:2)

1首先在您的应用中添加inappbrower

2为Google控制台创建应用ID   https://console.developers.google.com

a:创建新项目

b:点击凭证

c:选择网络应用程序

d:设置重定向路径     如果你没有设置http://localhost/callback

e:点击“创建”按钮  比弹出窗口显示保存那些id 之后添加以下代码

注意:请在代码中更改您的应用ID和密码ID

&#13;
&#13;
$scope.loginGoogle = function() {
	var requestToken = '';
	var accessToken = '';
	var clientId = '1018908884240-futc1bfc681kl2jegi3a7nn1m28aem1o.apps.googleusercontent.com';
	var clientSecret = 'KRQGDwu_llvagUucKM9oLZ7I';
            var deferred = $q.defer();
            $cordovaOauth.google(clientId, ['email']).then(function(result) {
                  $localStorage.accessToken = result.access_token;
                deferred.resolve(result.access_token);

            $http.get('https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=' + $localStorage.accessToken, {

                params: {
                    format: 'json'
                }
            }).then(function(result) {
	console.log(JSON.stringify(result));
	var id =result.data.id;
        
       deferred.resolve(result.data);
            }, function(error) {
                deferred.reject({
                    message: 'here was a problem getting your profile',
                    response: error
                });
            });


            }, function(error) {
                deferred.reject({
                    message: 'There was a problem signing in',
                    response: error
                });
            });
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

尝试将find添加到index.html文件中。 Cordova插件只能在仿真器或真实设备上运行。如果您想在浏览器中测试,请尝试Ripple Emulator

归功于Cordova plugins not working with ionic