piercetech/coinbase-oauth2

该包最新版本(0.1.0)没有可用的许可信息。

Coinbase OAuth 2.0 客户端提供者

0.1.0 2019-02-16 21:25 UTC

This package is auto-updated.

Last update: 2024-09-17 10:29:24 UTC


README

league/oauth2-client客户端实现的Coinbase API v2 OAuth 2.0提供者。

安装

要安装,请使用composer

composer require piercetech/coinbase-oauth2

用法

用法与普通客户端相同,使用PierceTech\OAuth2\Client\Provider\Coinbase作为提供者

授权码流程

$provider = new Openclerk\OAuth2\Client\Provider\Coinbase([
  'clientId'      => 'XXXXXXXX',
  'clientSecret'  => 'XXXXXXXX',
  'redirectUri'   => 'https://your-registered-redirect-uri/',
  'scopes'        => ['wallet:accounts:read', 'wallet:transactions:read', '...'],
]);

if (!isset($_GET['code'])) {

  // If we don't have an authorization code then get one
  $authUrl = $provider->getAuthorizationUrl();
  $_SESSION['oauth2state'] = $provider->state;
  header('Location: '.$authUrl);
  exit;

} else {

  // Try to get an access token (using the authorization code grant)
  $token = $provider->getAccessToken('authorization_code', [
      'code' => $_GET['code']
  ]);

  // Optional: Now you have a token you can look up a users profile data
  try {

    // We got an access token, let's now get the user's details
    $userDetails = $provider->getUserDetails($token);

    // Use these details to create a new profile
    printf('Hello %s!', $userDetails->data->firstName);

  } catch (Exception $e) {

    // Failed to get user details
    exit('Oh dear...');
  }

  // Use this to interact with an API on the users behalf
  echo $token->accessToken;

  // Use this to get a new access token when the old one expires (2 hours from initial handshake)
  echo $token->refreshToken;

  // The Unix Epoch Timestamp when the Coinbase Token will expire
  echo $token->expires;
}

刷新令牌

$provider = new PierceTech\OAuth2\Client\Provider\Coinbase([
  'clientId'      => 'XXXXXXXX',
  'clientSecret'  => 'XXXXXXXX',
  'redirectUri'   => 'https://your-registered-redirect-uri/',
]);

$grant = new \League\OAuth2\Client\Grant\RefreshToken();
$token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);

测试

由于这需要用户交互,没有进行实际的Coinbase交互测试,但可以运行一些基本组件质量测试

vendor/bin/phpunit