微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Twinfield API OAuth2.0 getaccessToken php-twinfield/twinfield

如何解决Twinfield API OAuth2.0 getaccessToken php-twinfield/twinfield

我目前正在尝试设置 twinfield API,使用 PHP-twinfield/twinfield 库时应该非常简单。但有一件事我并不完全理解。

这是我的代码

    $provider    = new OAuthProvider([
        'clientId'     => 'someClientId','clientSecret' => 'someClientSecret','redirectUri'  => 'https://example.org/'
    ]);

    $accesstoken  = $provider->getAccesstoken("authorization_code",["code" => ...]);
    $refreshToken = $accesstoken->getRefreshToken();
    $office       = \PHPtwinfield\Office::fromCode("someOfficeCode");

    $connection  = new \PHPtwinfield\Secure\OpenIdConnectAuthentication($provider,$refreshToken,$office);

$accesstoken 需要点上一些东西,某种代码。我不确定那应该是什么...

希望有人能帮帮我。已经谢谢了!


我仍然坚持使用 oauth2 设置...提供商似乎拥有它需要的所有信息。它返回检索 accesstoken 所需的代码。但是,尝试使用以下代码获得一个

$accesstoken = $provider->getAccesstoken('authorization_code',['code' => $_GET['code']]);

这将返回“invalid_grant”。 我试图重置我的 clientSecret ......但这没有帮助。 我希望有人能进一步帮助我。

解决方法

要访问 Twinfield API,用户必须经过身份验证。您可以通过指定用户名和密码或使用 OAuth2 来执行此操作。使用 OAuth2 时,您将身份验证委托给所谓的 OAuth 提供者。用户通过身份验证后,提供程序会将用户的浏览器重定向到您的应用程序中的端点 (redirectUri)。您的应用程序收到的该请求有一个名为 code 的 GET 参数。然后,您的应用程序将使用其 clientIdclientSecret 以及 HTTP POST 交换令牌的代码。这意味着您的应用程序必须在 OAuth2 提供商处注册,以便提供商(例如 github、facebook、google 等)可以验证客户端凭据并返回令牌。并且您必须将 provider 变量配置为指向您连接的 OAuth 提供程序。

$provider = new OAuthProvider([
    'clientId'                => 'XXXXXX',// The client ID assigned to you by the provider
    'clientSecret'            => 'XXXXXX',// The client password assigned to you by the provider
    'redirectUri'             => 'https://example.com/your-redirect-url/','urlAuthorize'            => 'https://login.provider.com/authorize',//where the user's browser should be redirected to for triggering the authentication
    'urlAccessToken'          => 'https://login.provider.com/token',//where to exchange the code for a token
    'urlResourceOwnerDetails' => 'https://login.provider.com/resource' //where to get more details about a user
]);

// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {

    // Fetch the authorization URL from the provider
    // Redirect the user to the authorization URL.
}

Twinfield 利用 league/oauth2-client 库来实现 OAuth。因此,有关如何在 twinfield 库中设置 OAuth 客户端的详细信息,请参阅 https://oauth2-client.thephpleague.com/usage/league/oauth2-client 支持一些开箱即用的提供程序并允许第三方提供程序。您的提供商可能在任何列表中。如果没有,请参阅您的提供商的文档以获取正确的网址。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。