我一直在爲此奮鬥了幾個小時,需要一些幫助。我創建了一個簡單的應用程序,用於在用戶將視圖重定向到Google Oauth頁面的角度視圖中向用戶顯示「使用Google登錄」按鈕。下面是調用當按鈕被按下登錄()函數的控制器代碼:OAuth令牌對角度視圖的響應(cookie)
angular.module('dashApp').controller('SigninCtrl', function ($scope) {
$scope.login=function() {
var client_id="191641883719-5eu80vgnbci49dg3fk47grs85e0iaf9d.apps.googleusercontent.com";
var scope="email";
var redirect_uri="http://local.host:9000/api/auth/google";
var response_type="code";
var url="https://accounts.google.com/o/oauth2/auth?scope="+scope+"&client_id="+client_id+"&redirect_uri="+redirect_uri+
"&response_type="+response_type;
window.location.replace(url);
};
});
在我的谷歌項目的重定向URI集重定向到一個服務器頁面,該服務器頁面:
'use strict';
var _ = require('lodash');
var request = require('request');
var qs = require('querystring');
var fs = require('fs');
// Get list of auths
exports.google_get = function (req,res){
var code = req.query.code,
error = req.query.error;
if(code){
//make https post request to google for auth token
var token_request = qs.stringify({
grant_type: "authorization_code",
code: code,
client_id: "191641883719-5eu80vgnbci49dg3fk47grs85e0iaf9d.apps.googleusercontent.com",
client_secret: process.env.GOOGLE_SECRET,
redirect_uri: "http://local.host:9000/api/auth/google"
});
var request_length = token_request.length;
var headers = {
'Content-length': request_length,
'Content-type':'application/x-www-form-urlencoded'
};
var options = {
url:'https://www.googleapis.com/oauth2/v3/token',
method: 'POST',
headers: headers,
body:token_request
};
request.post(options,function(error, response, body){
if(error){
console.error(error);
}else{
//WHAT GOES HERE?
}
});
}
if(error){
res.status(403);
}
}
我能夠成功地將谷歌返回的代碼交換爲授權令牌對象並將其記錄到終端。有人告訴我,我應該使用設置cookie:
res.setHeader('Content-Type', 'text/plain');
res.setCookie('SID','yes',{
domain:'local.host',
expires:0,
path:'/dashboard',
httpOnly:false
});
res.status(200);
res.end();
跟着我指引用戶在頁面上的控制器驗證會話。
我在做什麼錯?
我沒有看到你的問題是什麼。 – Mavlarn 2015-03-25 02:17:41
我意識到我需要讓角度控制器對服務器執行獲取請求,以實現瀏覽器的初始重新路由 - 現在就開始工作。一旦完成後我會發布代碼更新。 – 2015-03-25 03:36:20