我目前正在开发一个web特性,它是通过phpleague实现oauth2服务器的。我认为我在服务器的设计上做得很好,但是我不知道为什么客户端会显示一个我不明白的错误。
我的客户代码:
<?php
require 'vendor/autoload.php';
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => '1', // The client ID assigned to you by the provider
'clientSecret' => 'haha', // The client password assigned to you by the provider
'redirectUri' => 'http://localhost:4444/callback.php',
'urlAuthorize' => 'http://localhost:1337/authorize',
'urlAccessToken' => 'http://localhost:1337/access_token',
'urlResourceOwnerDetails' => 'https://service.example.com/resource'
]);
// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {
// Fetch the authorization URL from the provider; this returns the
// urlAuthorize option and generates and applies any necessary parameters
// (e.g. state).
$authorizationUrl = $provider->getAuthorizationUrl();
// Get the state generated for you and store it to the session.
$_SESSION['oauth2state'] = $provider->getState();
// Redirect the user to the authorization URL.
header('Location: ' . $authorizationUrl);
exit;
// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) {
if (isset($_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
}
exit('Invalid state');
} else {
try {
// Try to get an access token using the authorization code grant.
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
// We have an access token, which we may use in authenticated
// requests against the service provider's API.
echo 'Access Token: ' . $accessToken->getToken() . "<br>";
echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "<br>";
echo 'Expired in: ' . $accessToken->getExpires() . "<br>";
echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "<br>";
// Using the access token, we may look up details about the
// resource owner.
$resourceOwner = $provider->getResourceOwner($accessToken);
var_export($resourceOwner->toArray());
// The provider provides a way to get an authenticated API request for
// the service, using the access token; it returns an object conforming
// to Psr\Http\Message\RequestInterface.
var_dump($access_token);
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
// Failed to get the access token or user details.
exit($e->getMessage());
}
}
调用getAccessToken时回调中出现错误。
下面是堆栈跟踪:
Fatal error: Uncaught UnexpectedValueException:
Failed to parse JSON response:
Syntax error in /opt/lampp/htdocs/oauth2_client/vendor/league/oauth2-client/src/Provider/AbstractProvider.php:645
Stack trace:
#0 /opt/lampp/htdocs/oauth2_client/vendor/league/oauth2-client/src/Provider/AbstractProvider.php(686): League\OAuth2\Client\Provider\AbstractProvider->parseJson()
#1 /opt/lampp/htdocs/oauth2_client/vendor/league/oauth2-client/src/Provider/AbstractProvider.php(626): League\OAuth2\Client\Provider\AbstractProvider->parseResponse()
#2 /opt/lampp/htdocs/oauth2_client/vendor/league/oauth2-client/src/Provider/AbstractProvider.php(537): League\OAuth2\Client\Provider\AbstractProvider->getParsedResponse()
#3 /opt/lampp/htdocs/oauth2_client/callback.php(43): League\OAuth2\Client\Provider\AbstractProvider->getAccessToken()
#4 {main} thrown in /opt/lampp/htdocs/oauth2_client/vendor/league/oauth2-client/src/Provider/AbstractProvider.php on line 645
我不明白为什么我有这个错误..。
发布于 2021-12-09 10:55:29
我已经修正了错误。当我向我的access_token发送请求时,我注意到了错误,所以当我的客户端发送post请求并解析json时,它在解析时会导致一个错误,因为它试图直接将错误解析为字符串,而字符串不是json格式的。
https://stackoverflow.com/questions/70277730
复制相似问题