如何使用youtube api获取所有订阅的用户频道列表?
问题描述:
我使用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."
}
}
我曾尝试也使用卷曲呼叫,但也给了我同样的错误,任何建议将节省我的一天 我在代码中缺少什么?
答
首先,它需要一个认证的电话。
所以你需要让这个人通过Oauth2“验证”并收集令牌。
然后用令牌发送该呼叫
https://www.googleapis.com/youtube/v3/subscriptions?part=id,snippet,contentDetails&maxResults=50&channelId='.$channelId.'&access_token='.$access_token
然后你就可以访问JSON响应,并收集它们。
答
您的错误意味着您尚未设置Google API控制台项目。您正在访问的资源需要OAuth授权。您需要obtain authorization credentials in the Google Developers Console才能使用OAuth 2.0授权。
- 打开Credentials page。
-
该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