gpt4 book ai didi

javascript - ngCordova:oauth 与谷歌失败

转载 作者:行者123 更新时间:2023-12-03 09:10:47 24 4
gpt4 key购买 nike

我正在 ng-Cordova 上构建一个项目。我尝试了很多例子来使用谷歌进行日志记录。我的需要是使用谷歌帐户登录我的应用程序。我有登录按钮。当我单击按钮时,它需要重定向到谷歌帐户并使用谷歌帐户登录,然后需要返回到我的应用程序。我有一个谷歌客户ID。我特此发布我的index.html文件和app.js文件。

索引.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>

<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">

<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="js/ng-cordova.min.js"></script>
<script src="cordova.js"></script>

<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<body ng-app="starter">

<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content>
</ion-content>
</ion-pane>
</body>
</html>

我的 app.js 文件

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
var exampleApp = angular.module('starter', ['ionic', 'ngCordova']);

//.run(function($ionicPlatform) {
// $ionicPlatform.ready(function() {
//// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
//// for form inputs)
// if(window.cordova && window.cordova.plugins.Keyboard) {
// cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// }
// if(window.StatusBar) {
// StatusBar.styleDefault();
// }
var requestToken = "";
var accessToken = "";
var clientId = "";
var clientSecret = "";

var exampleApp = angular.module('example', ['ionic'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'templates/login.html',
controller: 'LoginController'
})
.state('secure', {
url: '/secure',
templateUrl: 'templates/secure.html',
controller: 'SecureController'
});
$urlRouterProvider.otherwise('/login');
});


exampleApp.controller('LoginController', function($scope, $http, $location) {

$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

$scope.login = function() {
var ref = window.open('https://accounts.google.com/o/oauth2/auth?client_id=' + clientId + '&redirect_uri=http://localhost/callback&scope=https://www.googleapis.com/auth/urlshortener&approval_prompt=force&response_type=code&access_type=offline', '_blank', 'location=no');
ref.addEventListener('loadstart', function(event) {
if((event.url).startsWith("http://localhost/callback")) {
requestToken = (event.url).split("code=")[1];
$http({method: "post", url: "https://accounts.google.com/o/oauth2/token", data: "client_id=" + clientId + "&client_secret=" + clientSecret + "&redirect_uri=http://localhost/callback" + "&grant_type=authorization_code" + "&code=" + requestToken })
.success(function(data) {
accessToken = data.access_token;
$location.path("/secure");
})
.error(function(data, status) {
alert("ERROR: " + data);
});
ref.close();
}
});
}

if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function (str){
return this.indexOf(str) == 0;
};
}

});

exampleApp.controller('SecureController', function($scope, $http) {

$scope.accessToken = accessToken;

});

在搜索了一些示例后,我另外创建了两个 html 页面。

登录.html

<ion-view ng-controller="LoginController" title="Oauth Login">
<ion-content>
<div>
<div class="padding">
<button ng-click="login()" class="button button-block button-stable">
Sign In
</button>
</div>
</div>
</ion-content>
</ion-view>

和 secure.html

<ion-view ng-controller="SecureController" title="Oauth Secure">
<ion-content>
<div>
<div class="padding">
{{accessToken}}
<button class="button button-block button-stable">
Do Something
</button>
</div>
</div>
</ion-content>
</ion-view>

当我运行应用程序时,仅显示带有空白页的索引文件。我不知道出了什么问题。有人可以帮助我吗?

最佳答案

嗨,在 github 中,我上传了一个示例项目以通过 google 登录在执行项目之前,请按照github的README.md文件中的步骤进行操作

关于javascript - ngCordova:oauth 与谷歌失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32076118/

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