2017-04-03 187 views
0

我使用Laravel的PHP,我有以下代碼:如何使用youtube api獲取所有訂閱的用戶頻道列表?

$client = new Google_Client(); 
     $client->setClientId(env('GOOGLE_ID')); 
     $client->setClientSecret(env('GOOGLE_SECRET')); 
     //$client->setRedirectUri($redirect_uri); 
     $client->addScope("https://www.googleapis.com/auth/youtube.force-ssl"); 
     $client->addScope("https://www.googleapis.com/auth/youtube"); 
     $client->addScope("https://www.googleapis.com/auth/youtube.readonly"); 
     $client->addScope("https://www.googleapis.com/auth/youtubepartner"); 

     $youtube = new \Google_Service_YouTube($client); 

     $searchResponse = $youtube->channels->listChannels('snippet', array('mine' => true)); 

     //$subscriptions = Curl::to('https://www.googleapis.com/youtube/v3/subscriptions')->withData(['part' => 'snippet', 'mine' => 'true'])->get(); 
     echo "<pre>"; 
     print_r($searchResponse); 

上面的代碼給了我以下錯誤:

Google_Service_Exception in REST.php line 118: 
{ 
"error": { 
    "errors": [ 
    { 
    "domain": "usageLimits", 
    "reason": "dailyLimitExceededUnreg", 
    "message": "Daily Limit for Unauthenticated Use Exceeded.  Continued use requires signup.", 
    "extendedHelp": "https://code.google.com/apis/console" 
}], 
"code": 403, 
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup." 


} 
} 

我曾嘗試也使用捲曲呼叫,但也給了我同樣的錯誤,任何建議將節省我的一天 我在代碼中缺少什麼?

回答

0

首先,它需要一個認證的電話。
所以你需要讓這個人通過Oauth2「驗證」並收集令牌。

然後用令牌發送該呼叫

https://www.googleapis.com/youtube/v3/subscriptions?part=id,snippet,contentDetails&maxResults=50&channelId='.$channelId.'&access_token='.$access_token 

然後你就可以訪問JSON響應,並收集它們。

0

您的錯誤意味着您尚未設置Google API控制檯項目。您正在訪問的資源需要OAuth授權。您需要obtain authorization credentials in the Google Developers Console才能使用OAuth 2.0授權。

  1. 打開Credentials page
  2. 該API支持API密鑰和OAuth 2.0憑據。在你的情況下,使用OAuth 2.0爲您的項目:

    • 的OAuth 2.0:您的應用程序必須發送一個OAuth 2.0令牌訪問私人用戶數據的任何請求。您的應用程序會發送一個客戶端ID,可能還有一個客戶端密鑰以獲取令牌。您可以爲Web應用程序,服務帳戶或已安裝的應用程序生成OAuth 2.0憑據。

      有關更多信息,請參閱Creating OAuth 2.0 credentials部分。

您還可以檢查此相關的主題:list user subscriptions to all youtube channels after getting access token

相關問題