benkle/deviantart

访问DeviantART的oAuth2 API

1.0.2 2017-11-09 18:35 UTC

This package is not auto-updated.

Last update: 2024-09-14 02:53:20 UTC


README

这个库可以用来访问[DeviantART官方oAuth2 API](https://www.deviantart.com/developers/),抽象出授权、请求构建和响应解析。好吧,至少是有点...

要求

安装

该库可以通过composer以常规方式包含

    composer require benkle/deviantart

用法

首先,你需要创建一个Seinopsys的oAuth提供者实例供库使用

use \SeinopSys\OAuth2\Client\Provider\DeviantArtProvider;

$provider = new DeviantArtProvider(
    [
        'clientId'     => 'YOUR ID',
        'clientSecret' => 'YOUR SECRET',
        'redirectUri'  => 'YOUR REDIRECT URL',
    ]
);

接下来,你需要获取一个存储的令牌。

// Must return an instance of \League\OAuth2\Client\Token\AccessToken or null
$accessToken = PROCURE_ACCESS_TOKEN();

现在,你可以用它们包裹Api类。

use \Benkle\Deviantart\Api;

$api = new Api($provider, $accessToken);

最后,授权!

use \Benkle\Deviantart\Exceptions\UnauthorizedException;

try {
    $scopes = [Api::SCOPE_BROWSE];
    $api->authorize($scopes);
    // authorize() will refresh the token, if necessary, so you might want to write it back to storage.
    // You can access it via $api->getAccessToken()
} catch (UnauthorizedException $e) {
    // Here you can handle the initial user input for authorization.
    // The exception message has been replaced with the URL you need to call, so you can get it easily like this:
    $url = "$e";
}

如果你在由初始授权调用的处理器中,你可以将授权码作为第二个参数传递给authorize

    $api->authorize($scopes, $authCode);

完成所有这些后,你可以使用API,例如像这样

try {
    /** @var \stdClass $result */
    $result = $api->browse()->getNewest();
} catch (ApiException $e) {
    // Handle API exception
}

端点根据文档分组到子对象中,并将返回\stdClass的实例。

这个规则的唯一例外是sta.sh提交,它返回用于添加部分的ApiRequest

use \Benkle\Deviantart\ApiRequestPart;

try {
    /** @var \stdClass $result */
        $result = $api
            ->stash()
            ->submit('Test', 'A sta.sh test', null, null,true)
            ->addPart(ApiRequestPart::from('test', fopen('/home/bizzl/test.txt', 'r'), 'test.txt'))
            ->send();
} catch (ApiException $e) {
    // Handle API exception
}

常见问题解答

  • 精心挑选的端点在哪里? 它们已被弃用,你不应该使用它们。
  • 为什么这么多端点都有$includeMature参数? 它们在文档中。我知道它们看起来没有意义,可能不会工作,但它们也不会真的伤害。
  • 除了一个端点外,所有端点都返回\stdClass。为什么? 这更容易进行类型提示,我不需要填充器和实体。毕竟,这个库是基本的,只是稍微面向对象。也许有一天我会围绕这个写一个更面向对象的库。