【问题标题】:Invalid_grant after page reload Twinfield页面重新加载后的 Invalid_grant Twinfield
【发布时间】:2021-07-29 15:43:11
【问题描述】:

我目前正在尝试设置 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');
        }

我希望有人能告诉我更多关于我做错了什么。已经感谢您的帮助。

【问题讨论】:

  • 我发现这可能与授权代码设置一次,然后 accesToken 和 refreshToken 在那之后被初始化有关。并在页面重新加载时跳过“if”并再次初始化 accessToken 和 refreshToken。尽管由于授权代码未更新,这些令牌与授权代码不匹配,因此未授予访问权限。有遇到过此类问题的人吗?
  • 授权码实际上是不允许多次使用的……所以每次重新加载页面时我都需要检索一个新的。然后用新代码检索一个新的 accessToken 来检索一个新的 refreshToken。

标签: php api oauth-2.0 access-token twinfield


【解决方案1】:

我使用会话计数器来确保每次刷新页面时都请求新的授权代码。这样授权码总是匹配访问令牌和刷新令牌。

<?php


namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use PhpTwinfield\ApiConnectors\CustomerApiConnector;
use PhpTwinfield\Secure\Provider\OAuthProvider;
use PhpTwinfield\Secure\OpenIdConnectAuthentication;


  class TWFconnector
  {
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        session_start();
        if (!isset($_SESSION['arr']['counter'])) {
            $_SESSION['arr']['counter'] = 0;
        } else {
            $_SESSION['arr']['counter']++;
        }

    $twin_client_id = '';
    $twin_client_secret = '';
    $twin_client_uri = '';

    $provider    = new OAuthProvider([
        'clientId'     => $twin_client_id,
        'clientSecret' => $twin_client_secret,
        'redirectUri'  => $twin_client_uri,
    ]);

    if (!isset($_GET['code'])) {
        $options = [
            'scope' => ['twf.user', 'twf.organisation', 
    'twf.organisationUser', 'offline_access', 'openid']
        ];

        $authorizationUrl = $provider->getAuthorizationUrl($options);

        header('Location: ' . $authorizationUrl);
        $_SESSION['arr']['counter'] = 0;
    } elseif($_SESSION['arr']['counter'] > 1) {
        header('Location: http://127.0.0.1:8000/');
    } else {
        $accessToken = $provider->getAccessToken('authorization_code', [
            'code' => $_GET['code']
        ]);

        $refreshToken = $accessToken->getRefreshToken();

        $office       = \PhpTwinfield\Office::fromCode("");
        $connection = new OpenIdConnectAuthentication($provider, 
        $refreshToken, $office);

            print_r($connection);
        }
        return $next($request);
    }
}

【讨论】:

    猜你喜欢
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 2013-11-09
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多