Friday, 8 November 2019

How to Create Broadcast in Channel with Youtube API in Python?

I am trying to schedule a live broadcast on youtube using the Python API. The broadcast is for a channel and my user has permissions to manage that channel. I need to use the "delegated_credentials" because is it inside a GSuite Organization with restrictions. To do so, I am using the following code:

# This OAuth 2.0 access scope allows for full read/write access to the
# authenticated user's account.
YOUTUBE_READ_WRITE_SCOPE = ["https://www.googleapis.com/auth/youtube", "https://www.googleapis.com/auth/youtube.upload", "https://www.googleapis.com/auth/youtube.force-ssl"]
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

def get_authenticated_service():

    credentials = service_account.Credentials.from_service_account_file(CLIENT_SECRETS_FILE, scopes=YOUTUBE_READ_WRITE_SCOPE)

    delegated_credentials = credentials.with_subject('example@email.com')

    return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, credentials=delegated_credentials)


# Create a liveBroadcast resource and set its title, scheduled start time,
# scheduled end time, and privacy status.
def insert_broadcast(youtube, options):
    print("Inserting broadcast...")
    insert_broadcast_response = youtube.liveBroadcasts().insert(part="snippet,status", body=dict(snippet=dict(title=options["broadcast_title"], scheduledStartTime=options["start_time"], scheduledEndTime=options["end_time"]),status=dict(privacyStatus=options["privacy_status"]))).execute()

    snippet = insert_broadcast_response["snippet"]

    print "Broadcast '%s' with title '%s' was published at '%s'." % (insert_broadcast_response["id"], snippet["title"], snippet["publishedAt"])

    return insert_broadcast_response["id"]


if __name__ == "__main__":

    options = {"broadcast_title": "New Broadcast", "start_time": '2020-01-30T00:00:00.000Z', "end_time": '2020-01-31T00:00:00.000Z', "privacy_status": "private"}
    youtube = get_authenticated_service()

    try:
        broadcast_id = insert_broadcast(youtube, args)
    except HttpError, e:
        print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)

However, I am getting the error:

An HTTP error 403 occurred:
{
 "error": {
  "errors": [
   {
    "domain": "youtube.liveBroadcast",
    "reason": "liveStreamingNotEnabled",
    "message": "The user is not enabled for live streaming.",
    "extendedHelp": "https://www.youtube.com/features"
   }
  ],
  "code": 403,
  "message": "The user is not enabled for live streaming."
 }
}

For what is seems, it is trying to create the broadcast in my user's account, with is in fact disabled because I don't have a personal channel, instead of creating in the channel I manage. How do I set up the credentials and application to upload to the correct channel?



from How to Create Broadcast in Channel with Youtube API in Python?

No comments:

Post a Comment