首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >页面重新加载Twinfield后的Invalid_grant

页面重新加载Twinfield后的Invalid_grant
EN

Stack Overflow用户
提问于 2021-05-07 18:21:02
回答 1查看 88关注 0票数 1

我目前正在尝试设置Twinfield API。我相信我就快到了,几乎...我设法检索了一个accessToken和一个refreshToken,但不知何故,这只在第一个页面加载时起作用。在重新加载页面后,我得到了'invalid_grant‘错误。我不是很确定哪里出了问题,因为它实际上是在第一次加载页面时工作。

下面是我的代码:

代码语言:javascript
复制
        $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');
        }

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-12 21:13:12

我使用了一个会话计数器来确保每次刷新页面时都会请求一个新的授权码。这样,授权码始终与访问令牌和刷新令牌匹配。

代码语言:javascript
复制
<?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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67433134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档