【问题标题】:Twinfield API OAuth2.0 getaccessToken php-twinfield/twinfieldTwinfield API OAuth2.0 getaccessToken php-twinfield/twinfield
【发布时间】:2021-07-28 11:36:01
【问题描述】:

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

【问题讨论】:

  • 使用这种类型的 OAuth2 重定向流程,您需要先将用户发送到登录提供程序;在他们使用他们的凭据授权后,他们会被重定向回您的应用程序(到您指定的 redirectUri),并在 URL 后附加一个 code GET 参数。然后可以将该代码交换为访问令牌。
  • 我不完全明白你的意思。我如何将用户发送到登录提供程序?这是否意味着我需要先设置 API 连接,然后将该连接发送给提供商?如果你有一个代码示例,那真的很有帮助! (不是说你需要为我做这项工作xD)
  • github.com/php-twinfield/twinfield#authentication指的是oauth2-client.thephpleague.com/usage,这基本上是你需要实现的。无论必要的 URL 端点是什么,您都需要通过 Twinfield API 文档弄清楚。 (除非您的 SDK 已经以某种方式以硬编码形式包含它们。)

标签: php laravel api oauth-2.0 twinfield


【解决方案1】:

要访问 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 支持一些开箱即用的提供程序并允许第三方提供程序。您的提供者可能在任何列表中。如果没有,请参阅您的提供商的文档以获取正确的 URL。

【讨论】:

  • 这让我更接近了一点,谢谢!虽然现在我不断收到“invalid_grant”错误。我不完全确定为什么......
猜你喜欢
  • 1970-01-01
  • 2022-06-24
  • 2021-07-29
  • 2022-06-13
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
相关资源
最近更新 更多