如何在PHP中将YouTube Analytic API V1升级到V2

时间:2019-02-28 13:49:47

标签: php youtube analytics

我正在尝试编写一个脚本,以获取特定频道中每个视频的YouTube分析报告。。我通过Composer安装了PHP Google Client Library,我认为这是最佳做法。我现在的问题是,当我运行代码时,我一直在出错,尽管我已经遵循了所有有关升级到API V2的文档,但我仍在使用APi的V1 ...我的代码如下

public function getVideoTrafficSource($videoId){
        if($this->session_available){
          $channelres = array();
          $msg = "";
          $key_dir = "../".file_dir."/".$_SESSION['my_user_name'].".txt";
               $result = true;
               if(file_exists($key_dir)){
                    $key = file_get_contents($key_dir);
                    $result = true;
               }else{
                    $result = false;
               }
  
               $client = new Google_Client();
               $client->setApplicationName($this->APPName);
               $client->setClientId($this->OAUTH2_CLIENT_ID);
               $client->setAccessType('offline');
               $client->setAccessToken($key);
               $client->setScopes($this->scope);
               $client->setClientSecret($this->OAUTH2_CLIENT_SECRET);
               if(!$result){
                  $msg = "No Key file available";
               }else{
                try{
                   if ($client->getAccessToken()) {
                       if($client->isAccessTokenExpired()) {
                           $newToken = json_decode($key);
                           $client->refreshToken($newToken->refresh_token);
                           //file_put_contents($key_dir, $client->getAccessToken());
                       }                
                       $youtube = new Google_Service_YouTubeAnalytics($client);
                         $trafficReport = $youtube->reports->query(array('ids'=> 'channel==UCRRUR9-KV3TyuFcZwhM_9Zw'),array('startDate'=> '2017-11-01'),array('endDate'=> '2018-11-01'),array('dimensions'=> 'insightTrafficSourceDetail'),array('metrics'=> 'estimatedMinutesWatched,views'),  array('filters'=> 'video==sDzJhtmvLKQ;insightTrafficSourceType==EXT_URL'),array('sort'=> '-estimatedMinutesWatched') );
                       //$trafficReport = $youtube->reports->query('2017-01-01','2018-01-01', 'views', 'insightTrafficSourceType', 'video=={$videoId}', 'views');
                         
                        }
                        $msg = $trafficReport;
                } catch(Google_Service_Exception $e) {
                          $msg = $e->getMessage();
                 }catch (Exception $e) {
                          $msg =  $e->getMessage();
                 }
               }
          }else{
            $msg = "Session Expired";
          }
          return $msg;
     }

我不知道我的代码是否有问题。这就是我到目前为止遇到的错误

error":"{\n \"error\": {\n  \"errors\": [\n   {\n    \"domain\": \"global\",\n    \"reason\": \"deleted\",\n    \"message\": \"Version 1 of the YouTube Analytics API has been deprecated as of November 1, 2018. Please refer to the migration guide for details about how to update your client to work with version 2 of the API: https:\/\/developers.google.com\/youtube\/analytics\/migration.\"\n   }\n  ],\n  \"code\": 410,\n  \"message\": \"Version 1 of the YouTube Analytics API has been deprecated as of November 1, 2018. Please refer to the migration guide for details about how to update your client to work with version 2 of the API: https:\/\/developers.google.com\/youtube\/analytics\/migration.\"\n }\n}\n"}

0 个答案:

没有答案