问题
I am trying to embed a live event video on my customer site using API. I am trying to retrieve the list of public livestream videos using youtube.liveBroadcasts.list.
It is working on the API Explorer query builder.
When I copy the request to the browser I am getting a login required
error.
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
The question is:
- Why do I need to authenticate if I am requesting the public live broadcasts? The API key is not enough?
- Is there an other way to get all user's public livestream events?
回答1:
That particular API endpoint doesn't just return public streams; it can return private ones as well (in addition to private info about public streams). For this reason, the authorization level sits in front of the endpoint, and so you must authenticate a user with oAuth2 (so the user grants explicit permission for you to be able to access that data).
If you are sure you'll only ever need public data about broadcasts, you should instead use the Data API's video search list endpoint. You can set the "channelId" parameter to the id of the user's channel, set the "type" parameter to "video," and then set the "eventType" parameter to either "completed," "live," or "upcoming." By using the "eventType" parameter, you'll have your search restricted to only broadcast events. So, for example, you might call a URL like this:
GET https://www.googleapis.com/youtube/v3/search?eventType=live&part=snippet&channelId=UCoMdktPbSTixAyNGwb-UYkQ&type=video&key={YOUR_API_KEY}
Unfortunately, there's no way to get completed, active, and upcoming broadcasts in the same API call, so if you need all 3 you'll have to make 3 calls.
回答2:
You can use the standard Search/list endpoint to return only live events from a particular channel, without being authenticated as that channel/user, if you know that channel's channelId
:
part -> snippet
channelId
-> [channelId of the channel/user with the live event]
eventType -> live
type -> video
(required when setting eventType
to live
)
HTTP GET https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={channelId}&eventType=live&type=video&key={YOUR_API_KEY}
回答3:
if you check the page you linked youtube.liveBroadcasts.list you will notice it says Authorization.
Authorization
This request requires authorization with at least one of the following scopes (read more about authentication and authorization).
https://www.googleapis.com/auth/youtube.readonly
https://www.googleapis.com/auth/youtube
You can't use a public key with this. You must be authenticated using Oauth2 to access youtube.liveBroadcasts.list.
来源:https://stackoverflow.com/questions/30343459/youtube-api-live-broadcast