gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:12:40 25 4
gpt4 key购买 nike

我正在尝试通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com