試了server端的java,一直卡在呼叫google ssl憑證失效的問題,還未找到google java api要如何調整,因此先調成javascript呼叫,試出來的流程
1.登入:連結此段
document.location.href = “https://accounts.google.com/o/oauth2/auth?response_type=token&client_id={ 向google申請的client id }&redirect_uri={ 向google申請欲導向的網頁url }&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email";
response_type原本是傳code,會在google認證完後將資料傳到設定欲導向的網頁url,並在url後帶參數code (Ex. {url}?code=……),此段一直在研究如何透過此code再向google取得access token,後來發現只要將此參數帶入token值,即會直接回傳access token,且若已登入按過允許此應用程式存取,則不用再登入,而直接連至欲導向的網頁url,並將access token帶至hash (Ex. {url}#code=…..)
2.存取客戶留存在google的資料
javascript 如下
function getPageParam(paramid) {
var rName = new RegExp("(\\?(.*))?$");
var getcatch = document.location.hash.replace('#','');
var t = getcatch.match(/([^&=]+)=([^=&]+)/g);
for (var l = 0; l < t.length; l++){
var r = t[l];
var tt = r.match(/([^&=]+)=([^=&]+)/);
if (tt && tt[1]==paramid)
return tt[2];
}
}
function validateToken() {
$.ajax({
url: 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + getPageParam("access_token"),
data: null,
success: function(response){
getUserInfo();
},
error: function(error) {
console.log('Our token is not valid....');
},
dataType: "jsonp"
});
}
function getUserInfo() {
$.ajax({
url: 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + getPageParam("access_token"),
data: null,
success: function(response) {
$('#user').html(response.name);
},
dataType: "jsonp"
});
}
validateToken();

請問第二段的完整html該怎麼寫呢?如何跟第一段串起來?因為是Ajax新手請多包涵
讚讚