sr1871/yii2-youtube-api

在yii2中使用Oauth访问YouTube API的组件

安装: 457

依赖项: 1

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 2

开放问题: 0

类型:yii2-extension

dev-master 2018-08-30 23:43 UTC

This package is not auto-updated.

Last update: 2024-09-22 04:15:18 UTC


README

YouTube API是yii2的一个扩展,用于将YouTube API作为yii2组件使用。

此扩展通过oauth访问YouTube API,保存访问令牌并在过期时刷新它,因此您只需一次提供凭证

此扩展还包含合作伙伴的方法,因此您无需在每次调用时都指定自己的onBehalfContentOwner,并且可以控制每分钟YouTube合作伙伴调用的次数,以避免超过每分钟调用限制

使用此扩展,您可以

  • 上传和更新视频
  • 上传视频缩略图
  • 视频列表
  • 创建和更新播放列表
  • 获取、添加和删除播放列表中的元素
  • 搜索
  • 放置播放器

还可以执行以下合作伙伴操作

  • 获取频道
  • 货币化/取消货币化视频

安装

安装此扩展的首选方法是通过composer。

$ composer require sr1871/yii2-youtube-api

使用

将其添加到您的组件中

添加到您的组件中

'components' => [
    ...
    'youtube' => [
        'class' => \sr1871\youtubeApi\components\YoutubeApi::className(),
        'clientId' => '{your Oauth Client Id, you can get it from google console}',
        'clientSecret' => '{your Oauth Client Secret, you can get it from google console}',
        'setAccessTokenFunction' => function($client){ file_put_contents('pathFile.txt'json_encode($client->getAccessToken());}, //anonymous function where save the accesToken
        'getAccessTokenFunction' => function(){ return file_get_contents('pathFile.txt');}, // an anonymous function where get the accessToken 
        'scopes' => ['{scopes that you going to use}', '{as array}'],
    ],
    ...
]

setAccessTokenFunctiongetAccessTokenFunction 非常重要,在一个中您将保存您的accessToken,在另一个中让组件取用它。

在上面的示例中,访问令牌被保存在txt中,在取回它的函数中返回该文件的内容。

重要的是您必须让 setAccessTokenFunction 有一个参数(例如 $client),并且始终只保存 json_encode($client->getAccessToken)

生成您的访问令牌

此组件的优点是您只需生成一次访问令牌。

在任何控制器中创建一个操作

public function actionValidation() {
    if(Yii::$app->request->get('code')){
        Yii::$app->youtube->validationPost(Yii::$app->urlManager->createAbsoluteUrl('/site/validation'));
    } else {
        Yii::$app->session->setFlash('success', 'The access token was generated');
        return $this->redirect('index');
    }
}

您可以根据需要调用您的操作,当有一个名为 'code' 的GET参数时,您必须调用 Yii::$app->youtube->validationPost('{url_to_this_action}') ,此方法创建并保存访问令牌并将重定向到作为参数传递的URL。

要获取此操作,您必须通过 Yii::$app->youtube->validationGet(Yii::$app->urlManager->createAbsoluteUrl('{url_to_action}')) 来做,例如,您可以在一个 <a> 标签中使用它

例如

echo Html::a('验证', Yii::$app->youtube->validationGet(Yii::$app->urlManager->createAbsoluteUrl('/site/validation')))

访问令牌将被保存,您可以使用此组件

示例

Yii::$app->youtube->setParts(['snippet', 'recordingDetails', 'id'])>listVideos(['id' => 'someId'])

您可以将您想要的部件传递给 setParts(),如果您不希望使用默认部件。有关每个方法和如何使用的更多信息,请参阅组件方法的 PhpDOC。

合作伙伴

如果您想将扩展用作合作伙伴,必须在您的配置中指定您的 onBehalfContentOwner

'youtube' => [
    'class' => \sr1871\youtubeApi\components\YoutubeApi::className(),
    'clientId' => '{your Oauth Client Id, you can get it from google console}',
    'clientSecret' => '{your Oauth Client Secret, you can get it from google console}',
    'setAccessTokenFunction' => function($client){ file_put_contents('pathFile.txt'json_encode($client->getAccessToken());}, //anonymous function where save the accesToken
    'getAccessTokenFunction' => function(){ return file_get_contents('pathFile.txt');}, // an anonymous function where get the accessToken 
    'scopes' => ['{scopes that you going to use}', '{as array}'],
    'onBehalfContentOwner' => {your_content_owner},
    'youtubePartnerCallsPerSecond' => 2 //you can indicate how many calls per second can you do, default is 2
],

如果您不知道您的 onBehalfContentOwner,您可以使用 getOnBehalfOfContentOwner() 方法获取它。

播放器

此扩展包括一个以小部件格式提供的播放器。

\sr1871\youtubeApi\widgets\YoutubeIFrame::widget([
    'id' => 'SomeIdForDivAndJSObject'
    'iFrameOptions' => [
        'videoId' => 'someId'
        ...
    ],
    'iFrameEvents' => [
        'onReady' => 'function(event) {
            console.log('ready')
        }'
    ],
    'options' => [] //html div options
])