[英]PHP PEAR mail fails using Google Apps mail account - error 535 authentication failed
[英]Google Authentication using PHP Fails
我正在嘗試允許用戶使用 Google 的 PHP 庫登錄我的網站。 他們的例子是Here 。 但是,使用他們提供的代碼,我每次都會遇到相同的錯誤。 我的第一個文件是 signin.php :
<?php
require_once __DIR__.'/google-api-php-client/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/authenticate.php');
$client->addScope("profile");
$client->addScope("email");
$client->addScope("openid");
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/authenticate.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
第二個文件是authenticate.php:
<?php
require_once __DIR__.'/google-api-php-client/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/authenticate.php');
$client->addScope("profile");
$client->addScope("email");
$client->addScope("openid");
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$yo = $client->authenticate($_GET['code']);
print_r($yo);
//$_SESSION['access_token'] = $client->getAccessToken();
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
print_r($token);
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/signin.php';
//header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
每次運行此程序時,我都會選擇我的帳戶並返回到該頁面。 返回了必要的代碼,但無法對其進行身份驗證,並出現以下錯誤:
Array ( [error] => invalid_request [error_description] => Could not determine client ID from request. ) Array ( [error] => invalid_request [error_description] => Could not determine client ID from request. )
手動生成client_secrets.json
並確保路徑正確。
當您使用 header(location:...) 重定向時,您建立的會話數據將丟失。 在重定向回應用程序后嘗試對會話變量執行 var_dump,您應該看到它是空的,至少在我的情況下是這樣。 你所做的一切在我看來都是正確的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.