我目前正在尝试设置 Twinfield API。我相信我快到了,几乎……我设法检索了一个 accessToken 和一个 refreshToken,但不知何故,这只适用于第一页加载。重新加载页面后,我收到“invalid_grant”错误。我不确定出了什么问题,因为它实际上正在加载第一页。
这是我的代码:
$provider = new OAuthProvider([
'clientId' => 'clientId',
'clientSecret' => 'clientSecret',
'redirectUri' => 'http://127.0.0.1:8000'
]);
if (!isset($_GET['code'])) {
$options = [
'scope' => ['twf.user', 'twf.organisation', 'twf.organisationUser', 'offline_access', 'openid']
];
$authorizationUrl = $provider->getAuthorizationUrl($options);
$_SESSION['oauth2state'] = $provider->getState();
header('Location: ' . $authorizationUrl);
exit;
} elseif (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) {
if (isset($_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
}
exit('Invalid state');
} else {
try {
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
$refreshToken = $accessToken->getRefreshToken();
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>";
$office = \PhpTwinfield\Office::fromCode("someOfficeCode");
$connection = new \PhpTwinfield\Secure\OpenIdConnectAuthentication($provider, $refreshToken, $office);
// $offices = $officeApiConnector->listAllWithoutOfficeCode();
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
// Failed to get the access token or user details.
exit($e->getMessage() . ' error');
}
我希望有人能告诉我更多关于我做错了什么。感谢您的帮助。