admin管理员组文章数量:1331883
I am working in client side angularjs.I am try to implement google oauth2. I am getting accesstoken but i need to get id_token.
I added app.js , controller.js and html part.
I followed this tutorial: /
app.js:
angular
.module('angularoauthexampleApp', [ ])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/access_token=:accessToken', {
template: '',
controller: function ($location,$rootScope) {
var hash = $location.path().substr(1);
var splitted = hash.split('&');
var params = {};
for (var i = 0; i < splitted.length; i++) {
var param = splitted[i].split('=');
var key = param[0];
var value = param[1];
params[key] = value;
$rootScope.accesstoken=params;
}
$location.path("/about");
}
})
.otherwise({
redirectTo: '/'
});
});
controller.js
angular.module('angularoauthexampleApp')
.controller('MainCtrl', function ($scope) {
$scope.login=function() {
var client_id="your client_id";
var scope="email";
var redirect_uri="http://localhost:9000";
var response_type="token";
var url="="+scope+"&client_id="+client_id+"&redirect_uri="+redirect_uri+
"&response_type="+response_type;
window.location.replace(url);
};
});
html:
<button class="btn btn-primary" ng-click="login()">Login</button>
I am working in client side angularjs.I am try to implement google oauth2. I am getting accesstoken but i need to get id_token.
I added app.js , controller.js and html part.
I followed this tutorial: http://anandsekar.github.io/oauth2-with-angularjs/
app.js:
angular
.module('angularoauthexampleApp', [ ])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/access_token=:accessToken', {
template: '',
controller: function ($location,$rootScope) {
var hash = $location.path().substr(1);
var splitted = hash.split('&');
var params = {};
for (var i = 0; i < splitted.length; i++) {
var param = splitted[i].split('=');
var key = param[0];
var value = param[1];
params[key] = value;
$rootScope.accesstoken=params;
}
$location.path("/about");
}
})
.otherwise({
redirectTo: '/'
});
});
controller.js
angular.module('angularoauthexampleApp')
.controller('MainCtrl', function ($scope) {
$scope.login=function() {
var client_id="your client_id";
var scope="email";
var redirect_uri="http://localhost:9000";
var response_type="token";
var url="https://accounts.google./o/oauth2/auth?scope="+scope+"&client_id="+client_id+"&redirect_uri="+redirect_uri+
"&response_type="+response_type;
window.location.replace(url);
};
});
html:
<button class="btn btn-primary" ng-click="login()">Login</button>
Share
Improve this question
asked Jun 24, 2016 at 10:30
RSKMRRSKMR
1,8925 gold badges32 silver badges74 bronze badges
3 Answers
Reset to default 5You need to use nonce. Add it and id_token will be in response.
For example:
let params = {
'client_id': GOOGLE_API_CLIEND_ID,
'redirect_uri': `${location.origin}/auth/google`,
'response_type': 'id_token token',
'scope': GOOGLE_API_SCOPES,
'state': 'af0ifjsldkj',
'nonce': 'n-0S6_WzA2Mj'
};
For implicit flow nonce param is required. For more information you can check http://openid/specs/openid-connect-core-1_0.html#ImplicitAuthRequest
To receive an id_token, you need to change your response_type param to:
var response_type="id_token";
as the response you will get an id_token. If you need both - the id_token and access_token, you should add "token" to response_type:
var response_type="token id_token";
To learn more, read OpenId article
Also you could test auth flow using Google Ouath Playground
To trigger an OpenID Connect flow, which is an extension of OAuth 2.0, you need to add the "openid" scope in the authentication request (and urlencode the space in between), so:
var scope="openid%20email";
本文标签: javascriptGoogle oauth2 get idtokenStack Overflow
版权声明:本文标题:javascript - Google oauth2 get id_token - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742271042a2444309.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论