我想從 Google OAuth 2.0 中檢索 Auth 令牌(我使用了本教程)但是,當我想進行身份驗證時,它會導致無限循環重定向到無,從而刷新頁面。沒有任何錯誤信息。我不知道出了什么問題。這是我的 PHP 代碼:<?php// Admin Google API settings// Portal url:require_once('./curl.php');define("CALLBACK_URL", "http://localhost/losapi/index2.php"); //Callback URLdefine("AUTH_URL", "https://accounts.google.com/o/oauth2/v2/auth"); //Used to get CODE (not Token!)define("CLIENT_ID", "***"); // Personaldefine("CLIENT_SECRET", "***"); // Personaldefine("SCOPE", "https://www.googleapis.com/auth/admin.directory.device.chromeos https://www.googleapis.com/auth/admin.directory.user https://www.googleapis.com/auth/admin.directory.orgunit"); // Depends on what you want to do.define("APIURL_DIRECTORY","https://www.googleapis.com/admin/directory/v1/customer/"); // For Google Directory actionsdefine("CUSTOMER_ID","***"); // Personaldefine("TOKEN_URL","https://oauth2.googleapis.com/token"); // URL to get Token (not code).$curl = new \CURL\cURL();// Initiate code for access tokenif(isset($_GET["code"])){ //DEBUG: echo "Code: ".$_GET["code"]; $url = TOKEN_URL."?"; $url .= "code=".$_GET["code"]; $url .= "&grant_type=authorization_code"; $url .= "&client_id=". urlencode(CLIENT_ID); $url .= "&client_secret=". urlencode(CLIENT_SECRET); $url .= "&redirect_uri=". urlencode(CALLBACK_URL); $response = json_decode($curl->exeCurl($url,"POST"), true); if(isset($response)){ if(array_key_exists("access_token", $response)) { $access_token = $response; setcookie("LOStoken", $response['access_token'], time() + (86400 * 30), "/"); // 86400 = 1 day } }} else { if(isset($_POST['gettoken'])){ $url = AUTH_URL."?"; $url .= "response_type=code"; $url .= "&client_id=". urlencode(CLIENT_ID); $url .= "&scope=". urlencode(SCOPE); $url .= "&redirect_uri=". urlencode(CALLBACK_URL); echo $curl->exeCurl($url,"GET"); }}
1 回答
繁花如伊
TA貢獻2012條經驗 獲得超12個贊
您正在嘗試通過 cURL 獲取身份驗證 URL - 這不起作用,此授權流程需要用戶交互。您需要在用戶的瀏覽器中將用戶重定向到此 URL。
您可以自動將用戶重定向到該 URL;或者你只是把它放到href一個鏈接的屬性中,這樣用戶就可以點擊它,開始整個過程。(我一般會推薦第二個選項,但至少在開發過程中是這樣。使用自動重定向,如果出現任何問題,您很有可能會再次創建循環重定向。)
- 1 回答
- 0 關注
- 133 瀏覽
添加回答
舉報
0/150
提交
取消
