mitchwilliamson/oauth2-youtube

league/oauth2-client的YouTube OAuth2提供者

dev-master 2016-09-28 07:32 UTC

This package is not auto-updated.

Last update: 2024-09-28 19:38:05 UTC


README

league/oauth2-client的YouTube OAuth2提供者。

用于获取YouTube频道的详细信息(频道名称、个人资料图片等)。

安装

使用composer安装此包

composer require mitchwilliamson/oauth2-youtube

用法

$provider = new \League\OAuth2\Client\Provider\YouTube([
    'clientId'                => '{youtube-client-id}',    // The client ID assigned to you by Google/YouTube
    'clientSecret'            => '{youtube-client-secret}',   // The client secret assigned to you by Google/YouTube
    'redirectUri'             => 'http://example.com/your-redirect-url/',
]);

// If we don't have an authorization code then get one
if (!isset($_GET['code'])) {

    // Fetch the authorization URL from the provider; this returns the
    // urlAuthorize option and generates and applies any necessary parameters
    // (e.g. state).
    $authorizationUrl = $provider->getAuthorizationUrl();

    // Get the state generated for you and store it to the session.
    $_SESSION['oauth2state'] = $provider->getState();

    // Redirect the user to the authorization URL.
    header('Location: ' . $authorizationUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    unset($_SESSION['oauth2state']);
    exit('Invalid state');

} else {

    try {

        // Try to get an access token (using the authorization code grant)
        $token = $provider->getAccessToken('authorization_code', [
            'code' => $request->input('code')
        ]);
        // Using the access token, we may look up details about the
        // resource owner.
        $resourceOwner = $provider->getResourceOwner($accessToken);

        var_export($resourceOwner->toArray());

        // You can also use these functions:
        $channelId = $resourceOwner->getId();
        $channelName = $resourceOwner->getName();
        $channelAvatar = $resourceOwner->getImageurl();
        $channelDescription = $resourceOwner->getDescription();
        
    } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {

        // Failed to get the access token or user details.
        exit($e->getMessage());

    }

}

贡献

目前此包非常基础。任何贡献都欢迎通过pull request形式! :)

待办事项

  • 处理异常
  • 提供更多功能

致谢

许可证

MIT许可证(MIT),(详细信息请见LICENSE文件)。