找回密碼
 註冊
搜索
查看: 92|回復: 0

[教學] 如何在網頁加入google登入 auth

[複製鏈接]
發表於 昨天 17:48 | 顯示全部樓層 |閱讀模式
Push to Facebook
本帖最後由 pipe 於 2026-3-13 19:46 編輯

1. 申請 google 授權碼 (Auth Code)
說明文件

建立用戶端 ID 和用戶端密碼
如要建立用戶端 ID 和用戶端密鑰,請建立 Google API 控制台專案、設定 OAuth 用戶端 ID,並註冊 JavaScript 來源:
  • 從專案下拉式選單中選取現有專案,或選取「Create a new project」建立新專案。
    注意: 請使用單一專案來保留應用程式的所有平台例項 (Android、iOS、網頁等),每個例項都應有不同的用戶端 ID。
  • 在側欄的「API 和服務」下方,選取「憑證」,然後按一下「設定同意畫面」。
    選擇電子郵件地址、指定產品名稱,然後按下「儲存」。
  • 在「Credentials」分頁中,選取「Create credentials」下拉式清單,然後選擇「OAuth client ID」。
  • 在「Application type」(應用程式類型) 下方,選取 [Web application] (網頁應用程式)。
    註冊應用程式可存取 Google API 的來源,如下所示。來源是指通訊協定、主機名稱和通訊埠的獨特組合。

    • 在「Authorized JavaScript origins」(已授權的 JavaScript 來源) 欄位中輸入應用程式的來源。您可以輸入多個來源,讓應用程式在不同的通訊協定、網域或子網域上執行。請勿使用萬用字元。在下方範例中,第二個網址可能是正式版網址。
    • 「已授權的重新導向 URI」欄位不需要值。Redirect URI 不適用於 JavaScript API。
    • 按下「Create」(建立) 按鈕,


  • 在隨即顯示的「OAuth 用戶端」對話方塊中,複製用戶端 ID。用戶端 ID 可讓應用程式存取已啟用的 Google API。

    google登入 auth

    google登入 auth





2. 以 https://kingliwei.com 網域為例
建立 login.php


  1. <?php
  2. include "global.php";
  3. session_start();        
  4. // Google API 設定
  5. $client_id = "your_id.apps.googleusercontent.com";

  6. if (isset($_POST['credential'])) {
  7.     $id_token = $_POST['credential'];
  8.    
  9.     // 改用 cURL 驗證,比 file_get_contents 更穩定
  10.     $ch = curl_init();
  11.     curl_setopt($ch, CURLOPT_URL, "https://oauth2.googleapis.com/tokeninfo?id_token=" . $id_token);
  12.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  13.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 若伺服器 SSL 憑證過舊,此行可避免驗證無效
  14.     $response = curl_exec($ch);
  15.     curl_close($ch);
  16.    
  17.     $user_data = json_decode($response, true);
  18.     if (isset($user_data['email'])) {
  19.         $email = addslashes($user_data['email']);
  20.         
  21.         // 檢查資料庫 (請確保 net_member 資料表有 email 欄位)
  22.         $sql = "SELECT * FROM net_member WHERE email = '$email'";
  23.         $query = $db->query($sql);
  24.         $row = $db->fetch_array($query);

  25.         if ($row) {
  26.             // 登入成功:先設定 Session,最後再執行跳轉
  27.             $_SESSION['username'] = $row['username'];
  28.             $_SESSION['uid'] = $row['uid'];
  29.             
  30.             echo "<script>alert('Google 登入成功!');window.location='index.php';</script>";
  31.             exit;
  32.         } else {
  33.             // 沒帳號,帶到註冊頁
  34.             echo "<script>alert('尚未註冊,請先完成帳號綁定。');window.location='reg.php?email=$email';</script>";
  35.             exit;
  36.         }
  37.     } else {
  38.         echo "<script>alert('Google 驗證無效');history.go(-1);</script>";
  39.         exit;
  40.     }
  41. }

  42. $smarty->display('login.htm');
  43. mysqli_close();
  44. ?>
複製代碼


3. 建立 login.htm

  1. <div id="g_id_onload"
  2.                                          data-client_id="your_id.apps.googleusercontent.com"
  3.                                          data-callback="handleCredentialResponse"
  4.                                          data-ux_mode="popup"
  5.                                          data-auto_prompt="false">
  6.                                 </div>
  7.                                 <div class="g_id_signin"
  8.                                          data-type="standard"
  9.                                          data-shape="rectangular"
  10.                                          data-theme="outline"
  11.                                          data-text="signin_with"
  12.                                          data-size="large"
  13.                                          data-logo_alignment="left">
  14.                                 </div>

  15. <script src="https://accounts.google.com/gsi/client" async defer></script>

  16. <script>
  17.   function handleCredentialResponse(response) {
  18.     // 建立一個表單自動提交
  19.     var form = document.createElement('form');
  20.     form.setAttribute('method', 'post');
  21.     form.setAttribute('action', 'login.php');

  22.     var hiddenField = document.createElement('input');
  23.     hiddenField.setAttribute('type', 'hidden');
  24.     hiddenField.setAttribute('name', 'credential'); // 這裡要對應後端的 $_POST['credential']
  25.     hiddenField.setAttribute('value', response.credential);

  26.     form.appendChild(hiddenField);
  27.     document.body.appendChild(form);
  28.     form.submit();
  29.   }
  30. </script>
複製代碼


展示圖

google登入 auth

google登入 auth

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|小黑屋|TShopping

GMT+8, 2026-3-14 03:13 , Processed in 0.022462 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回復 返回頂部 返回列表