我想从 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 URL
define("AUTH_URL", "https://accounts.google.com/o/oauth2/v2/auth"); //Used to get CODE (not Token!)
define("CLIENT_ID", "***"); // Personal
define("CLIENT_SECRET", "***"); // Personal
define("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 actions
define("CUSTOMER_ID","***"); // Personal
define("TOKEN_URL","https://oauth2.googleapis.com/token"); // URL to get Token (not code).
$curl = new \CURL\cURL();
// Initiate code for access token
if(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");
}
}
繁花如伊