網頁版 Google OAuth 的作法

1,279次阅读
没有评论

網頁版 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、百度等平台型巨头持续布局
文章搜索
热门文章
清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开

清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开

清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开 Jay 2026-01-08 20:18:...
手把手教你用AI 10分钟生成一个APP!零基础也能搞定

手把手教你用AI 10分钟生成一个APP!零基础也能搞定

今日,我将向大家展示DeepSeek的全新玩法——从零开始,利用AI创建一个完整的应用程序。借助DeepSee...
训具身模型遇到的很多问题,在数据采集时就已经注定了丨鹿明联席CTO丁琰分享

训具身模型遇到的很多问题,在数据采集时就已经注定了丨鹿明联席CTO丁琰分享

训具身模型遇到的很多问题,在数据采集时就已经注定了丨鹿明联席CTO丁琰分享 衡宇 2026-01-08 20:...
开源“裸考”真实世界,国产具身智能基座模型拿下全球第二!

开源“裸考”真实世界,国产具身智能基座模型拿下全球第二!

开源“裸考”真实世界,国产具身智能基座模型拿下全球第二! 西风 2026-01-08 19:02:20 来源:...
最新评论
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.
热评文章
NVIDIA 发布全新物理 AI 模型,全球合作伙伴展示新一代机器人

NVIDIA 发布全新物理 AI 模型,全球合作伙伴展示新一代机器人

NVIDIA 发布全新物理 AI 模型,全球合作伙伴展示新一代机器人 梦晨 2026-01-07 17:25:...
手把手教你用AI 10分钟生成一个APP!零基础也能搞定

手把手教你用AI 10分钟生成一个APP!零基础也能搞定

今日,我将向大家展示DeepSeek的全新玩法——从零开始,利用AI创建一个完整的应用程序。借助DeepSee...
清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开

清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开

清库存!DeepSeek突然补全R1技术报告,训练路径首次详细公开 Jay 2026-01-08 20:18:...
训具身模型遇到的很多问题,在数据采集时就已经注定了丨鹿明联席CTO丁琰分享

训具身模型遇到的很多问题,在数据采集时就已经注定了丨鹿明联席CTO丁琰分享

训具身模型遇到的很多问题,在数据采集时就已经注定了丨鹿明联席CTO丁琰分享 衡宇 2026-01-08 20:...