0

我目前正在尝试设置 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 ......但这没有帮助。我希望有人可以进一步帮助我。

4

1 回答 1

0

要访问 Twinfield API,用户必须经过身份验证。您可以通过指定用户名和密码或使用 OAuth2 来执行此操作。使用 OAuth2 时,您将身份验证委托给所谓的 OAuth 提供者。用户通过身份验证后,提供者会将用户的浏览器重定向到redirectUri您应用程序的端点 ( )。您的应用程序接收到的请求有一个名为code. clientId然后,您的应用程序将使用其和clientSecretHTTP 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支持一些开箱即用的提供程序并允许第三方提供程序。您的提供者可能在任何列表中。如果没有,请参阅您的提供商的文档以获取正确的 URL。

于 2021-05-06T09:08:15.203 回答