项目作者: Bogstag

项目描述 :
trakt.tv OAuth 2.0 Client Provider for The PHP League OAuth2-Client
高级语言: PHP
项目地址: git://github.com/Bogstag/oauth2-trakt.git
创建时间: 2017-02-25T07:54:59Z
项目社区:https://github.com/Bogstag/oauth2-trakt

开源协议:MIT License

下载


trakt.tv Provider for OAuth 2.0 Client

Latest Version
Build Status
Coverage Status
Quality Score
StyleCI
Total Downloads
Software License

This package provides trakt.tv OAuth 2.0 support for the PHP League’s OAuth 2.0 Client.

Installation

To install, use composer:

  1. composer require bogstag/oauth2-trakt

Usage

Usage is the same as The League’s OAuth client, using \Bogstag\OAuth2\Client\Provider\Trakt as the provider.

Authorization Code Flow

  1. $provider = new Bogstag\OAuth2\Client\Provider\Trakt([
  2. 'clientId' => '{trakt-client-id}',
  3. 'clientSecret' => '{trakt-client-secret}',
  4. 'redirectUri' => 'https://example.com/callback-url'
  5. ]);
  6. if (!isset($_GET['code'])) {
  7. // If we don't have an authorization code then get one
  8. $authUrl = $provider->getAuthorizationUrl();
  9. $_SESSION['oauth2state'] = $provider->getState();
  10. header('Location: '.$authUrl);
  11. exit;
  12. // Check given state against previously stored one to mitigate CSRF attack
  13. } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
  14. unset($_SESSION['oauth2state']);
  15. exit('Invalid state');
  16. } else {
  17. // Try to get an access token (using the authorization code grant)
  18. $token = $provider->getAccessToken('authorization_code', [
  19. 'code' => $_GET['code']
  20. ]);
  21. // Optional: Now you have a token you can look up a users profile data
  22. try {
  23. // We got an access token, let's now get the user's details
  24. $user = $provider->getResourceOwner($token);
  25. // Use these details to create a new profile
  26. printf('Hello %s!', $user->getName());
  27. } catch (Exception $e) {
  28. // Failed to get user details
  29. exit('Oh dear...');
  30. }
  31. // Use this to interact with an API on the users behalf
  32. echo $token->getToken();
  33. }

Testing

  1. $ ./vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.