android - 如何使用 ClientID 和 ClientSecret 在 Phonegap 中使用 Angularjs 登录 Google OAuth2

标签 android angularjs cordova google-oauth

我正在尝试通过 Google OAuth2 使用 Angularjs(使用 Ionic 框架)从我的 Phonegap 应用程序登录。目前我正在使用 http://phonegap-tips.com/articles/google-api-oauth-with-phonegaps-inappbrowser.html用于登录。但是当我为 Ionic 使用 Angular-UI-Router 时,它会创建非常丑陋的代码并且很难理解代码。

这个问题似乎在没有任何正确答案的情况下盘旋。我希望它现在应该得到解决。 Google Angular Guys 应该会有所帮助。 How to implement Google Auth in phonegap?

最接近的主题是How to use Google Login API with Cordova/Phonegap ,但这不是 angularjs 的解决方案。

我必须使用以下代码传输 javascript 变量值:

        var el = document.getElementById('test');
        var scopeTest = angular.element(el).scope();
        scopeTest.$apply(function(){ 
            scopeTest.user = user;
            scopeTest.logged_in = true;
            scopeTest.name = user.name;
            scopeTest.email = user.email;
        });

最佳答案

我做了这样的解决方案,其中 TestCtrl 是登录按钮所在的 Controller 。混合了基于 jquery 的 $.ajax 调用,我将更改为 angualar 方式。 google_call 函数基本上调用了 phonegap-tips 中上面提到的链接中提到的 google_api。

.controller('TestCtrl', function($scope,$ionicPopup) {
$scope.logged_in = false;
$scope.getMember = function(id) {
    console.log(id);
};
$scope.test = function(){
    $ionicPopup.alert({"title":"Clicked"});
}

$scope.call_google = function(){
    googleapi.authorize({
    client_id: 'CLIENT_ID',
    client_secret: 'CLIENT_SECRET',

    redirect_uri: 'http://localhost',
    scope: 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email'
    }).done(function(data) {
        accessToken=data.access_token;
        // alert(accessToken);
        // $loginStatus.html('Access Token: ' + data.access_token);
        console.log(data.access_token);
        //$ionicPopup.alert({"title":JSON.stringify(data)});
        $scope.getDataProfile();
    });
};
$scope.getDataProfile = function(){
    var term=null;
    //  alert("getting user data="+accessToken);
    $.ajax({
           url:'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token='+accessToken,
           type:'GET',
           data:term,
           dataType:'json',
           error:function(jqXHR,text_status,strError){
           },
           success:function(data)
           {
           var item;

           console.log(JSON.stringify(data));
           // Save the userprofile data in your localStorage.
           window.localStorage.gmailLogin="true";
           window.localStorage.gmailID=data.id;
           window.localStorage.gmailEmail=data.email;
           window.localStorage.gmailFirstName=data.given_name;
           window.localStorage.gmailLastName=data.family_name;
           window.localStorage.gmailProfilePicture=data.picture;
           window.localStorage.gmailGender=data.gender;
           window.localStorage.gmailName=data.name;
           $scope.email = data.email;
           $scope.name = data.name;
           }
        });
        //$scope.disconnectUser(); //This call can be done later.
};
$scope.disconnectUser = function() {
  var revokeUrl = 'https://accounts.google.com/o/oauth2/revoke?token='+accessToken;

  // Perform an asynchronous GET request.
  $.ajax({
    type: 'GET',
    url: revokeUrl,
    async: false,
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(nullResponse) {
      // Do something now that user is disconnected
      // The response is always undefined.
      accessToken=null;
      console.log(JSON.stringify(nullResponse));
      console.log("-----signed out..!!----"+accessToken);
    },
    error: function(e) {
      // Handle the error
      // console.log(e);
      // You could point users to manually disconnect if unsuccessful
      // https://plus.google.com/apps
    }
  });
};
})

我正在为尝试使用 Google OAuth2 登录时遇到类似问题的新手提供这个答案。所以无耻地要求 Upvotes 因为我也是新来的!

关于android - 如何使用 ClientID 和 ClientSecret 在 Phonegap 中使用 Angularjs 登录 Google OAuth2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24140840/

相关文章:

android - 如何将 ArrayList<Uri> 转换为字符串?

加载 Ng-Repeat 元素后运行的 jQuery Swiper 脚本

javascript - Symfony2 : manually submit a form without class via AJAX

angularjs - 如何在AngularJS的日期选择器文本框中设置今天的日期

node.js - 找不到模块 'internal/errors' ionic

javascript读取新的rss条目并创建通知

android - AAPT在使用构建工具的Debian Jenkins Server Gradle Build上失败24. +

java - Android中的线程问题

java - 项目运行时自动选择EditText

javascript - 在 Phonegap 中打开 PDF (Android)