简体   繁体   English

Twinfield API OAuth2.0 getaccessToken php-twinfield/twinfield

[英]Twinfield API OAuth2.0 getaccessToken php-twinfield/twinfield

I am currently trying to setup the Twinfield API, it should be pretty straight forward when using the php-twinfield/twinfield library.我目前正在尝试设置 Twinfield API,使用 php-twinfield/twinfield 库时应该非常简单。 But there is one thing I don't fully understand.但有一件事我并不完全理解。

Here is my code:这是我的代码:

    $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);

The $accessToken require something on the dots, some sort of code. $accessToken 需要点上的东西,某种代码。 I am not sure what that should be...我不确定那应该是什么...

I hope someone can help me out.我希望有人可以帮助我。 Thanks already!已经谢谢了!


I am still stuck with oauth2 setup... the provider seems to have all the information it needs to have.我仍然坚持使用 oauth2 设置......提供商似乎拥有它需要拥有的所有信息。 It returns a code which is needed to retrieve an accessToken.它返回检索 accessToken 所需的代码。 But, trying to get one using the following code:但是,尝试使用以下代码获取一个:

$accessToken = $provider->getAccessToken('authorization_code', 
  ['code' => $_GET['code']]);

This will return 'invalid_grant'.这将返回“invalid_grant”。 I have tried to reset my clientSecret... but that did not help.我试图重置我的 clientSecret ......但这没有帮助。 I hope somebody can help me any further.我希望有人可以进一步帮助我。

To access the Twinfield API the users must be authenticated.要访问 Twinfield API,用户必须经过身份验证。 You can either do this by specifying a username and password or using OAuth2.您可以通过指定用户名和密码或使用 OAuth2 来执行此操作。 When using OAuth2 you delegate the authentication to a so called OAuth Provider.使用 OAuth2 时,您将身份验证委托给所谓的 OAuth 提供程序。 After the user authenticated, the provider will redirect the user's browser to an endpoint ( redirectUri ) at your application.用户通过身份验证后,提供程序会将用户的浏览器重定向到您应用程序的端点 ( redirectUri )。 That request, that your application receives, has a GET parameter called code .您的应用程序接收到的请求有一个名为code的 GET 参数。 Your app will then exchange the code for a token using its clientId and clientSecret and HTTP POST.然后,您的应用将使用其clientIdclientSecret以及 HTTP POST 将代码交换为令牌。 Which means that your application must be registered at the OAuth2 provider so that the provider (eg github, facebook, google, ...) can validate the client credentials and return a token.这意味着您的应用程序必须在 OAuth2 提供商处注册,以便提供商(例如 github、facebook、google ......)可以验证客户端凭据并返回令牌。 And you will have to configure your provider variable to point to the OAuth provider that you connect with.而且您必须将您的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 makes use of league/oauth2-client library for implementing OAuth. Twinfield 使用 League league/oauth2-client库来实现 OAuth。 Therefore, refer to https://oauth2-client.thephpleague.com/usage/ for the details on how to setup an OAuth client in the twinfield library.因此,有关如何在 twinfield 库中设置 OAuth 客户端的详细信息,请参阅https://oauth2-client.thephpleague.com/usage/ league/oauth2-client supports some providers out of the box and allows third-party providers. league/oauth2-client支持一些开箱即用的提供者,并允许第三方提供者。 Your provider may be in any of the lists.您的提供者可能在任何列表中。 If not, refer to the documentation of your provider to get the right URLs.如果没有,请参阅您的提供商的文档以获取正确的 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM