網頁版 Google OAuth 的作法

1,246次阅读
没有评论

網頁版 Google OAuth 的作法

早期的作法是「Integrating Google Sign-In into your web app」這個,但官方已經標注 deprecated 了,在官方的文件上面可以看到對應的警告說明,現在雖然會動,但之後應該會關掉:

Warning: The support of Google Sign-In JavaScript platform library for Web is set to be deprecated after March 31, 2023. The solutions in this guide are based on this library and therefore also deprecated.

本來一開始是走「OAuth 2.0 for Client-side Web Applications」這個,這份文件會教你引入 Google 提供的 javascript library,也就是 https://apis.google.com/js/api.js 這份,但其實沒必要這樣用… 先不管會多吃多少資源,比較敏感的是隱私問題 (像是透過 3rd-party cookie 追蹤)。

後來研究出來比較好的方法是走「Using OAuth 2.0 for Web Server Applications」這邊提到的方式,就算是 javascript 也可以建立出來,像是這樣:

(() => {
const el = document.getElementById(‘glogin’);
el.addEventListener(‘click’, () => {
document.location = ‘https://accounts.google.com/o/oauth2/v2/auth?’ +
‘client_id=x-x.apps.googleusercontent.com’ +
‘&redirect_uri=https://test.example.com/google_redirect_uri.php’ +
‘&response_type=code’ +
‘&scope=profile+email’;
});
})();

然後這邊的接收端 (google_redirect_uri.php) 是讓 Google 授權後用 redirect 的方式把對應的認證變數 code 帶過來,這邊是用 PHP 實做,有兩個步驟:

  • 先用 POST 打 https://oauth2.googleapis.com/token 取得 (換得) access token,也就是 access_token
  • 再用 GET 打 https://www.googleapis.com/oauth2/v3/userinfo (帶上一個取得的 access token 進去) 取得使用者資料。

scope 裡如果沒有 email 的話,最後就不會拿到 email,但 SSO 應用應該會需要這個資訊,所以範例裡面還是放進去…

這邊是故意儘量用 PHP 內建的 library 實做,但應該不算複雜,換用 Guzzle 或是用其他語言應該算簡單:

$qs = http_build_query([
‘code’ => $_GET[‘code’],
‘client_id’ => ‘x-x.apps.googleusercontent.com’,
‘client_secret’ => ‘x’,
‘redirect_uri’ => ‘https://test.example.com/google_redirect_uri.php’,
‘grant_type’ => ‘authorization_code’,
]);

$url = ‘https://oauth2.googleapis.com/token’;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);

$data = json_decode($res);

$atoken = $data->access_token;

$url = ‘https://www.googleapis.com/oauth2/v3/userinfo’;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [“Authorization: Bearer ${atoken}”]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);

最後的資料就在 $res 裡面,想要看可以直接用 var_dump($res); 看。

這樣只有使用者真的要用 Google SSO 時才會有 request 進到 Google 伺服器。

Read More 

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2023 年 4 月
 12
3456789
10111213141516
17181920212223
24252627282930
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
“昆山杯”第二十七届清华大学创业大赛决赛举行

“昆山杯”第二十七届清华大学创业大赛决赛举行

“昆山杯”第二十七届清华大学创业大赛决赛举行 一水 2025-12-22 17:04:24 来源:量子位 本届...
MiniMax海螺视频团队首次开源:Tokenizer也具备明确的Scaling Law

MiniMax海螺视频团队首次开源:Tokenizer也具备明确的Scaling Law

MiniMax海螺视频团队首次开源:Tokenizer也具备明确的Scaling Law 一水 2025-12...
天下苦SaaS已久,企业级AI得靠「结果」说话

天下苦SaaS已久,企业级AI得靠「结果」说话

天下苦SaaS已久,企业级AI得靠「结果」说话 Jay 2025-12-22 13:46:04 来源:量子位 ...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
摩尔线程的野心,不藏了

摩尔线程的野心,不藏了

摩尔线程的野心,不藏了 量子位的朋友们 2025-12-22 10:11:58 来源:量子位 上市后的仅15天...
摩尔线程的野心,不藏了

摩尔线程的野心,不藏了

摩尔线程的野心,不藏了 量子位的朋友们 2025-12-22 10:11:58 来源:量子位 上市后的仅15天...
AI体育教练来了!中国团队打造SportsGPT,完成从数值评估到专业指导的智能转身

AI体育教练来了!中国团队打造SportsGPT,完成从数值评估到专业指导的智能转身

AI体育教练来了!中国团队打造SportsGPT,完成从数值评估到专业指导的智能转身 量子位的朋友们 2025...
AI体育教练来了!中国团队打造SportsGPT,完成从数值评估到专业指导的智能转身

AI体育教练来了!中国团队打造SportsGPT,完成从数值评估到专业指导的智能转身

AI体育教练来了!中国团队打造SportsGPT,完成从数值评估到专业指导的智能转身 量子位的朋友们 2025...
真正面向大模型的AI Infra,必须同时懂模型、系统、产业|商汤大装置宣善明@MEET2026

真正面向大模型的AI Infra,必须同时懂模型、系统、产业|商汤大装置宣善明@MEET2026

真正面向大模型的AI Infra,必须同时懂模型、系统、产业|商汤大装置宣善明@MEET2026 量子位的朋友...