YouTube

Activities: insert

experimental feature
The YouTube Data API (v3) is an experimental API version, which means it is still in development. We'll be adding many more features to the API while it is still an experimental version. Until the experimental label is removed, however, the Deprecation Policy for YouTube APIs won't apply to this version as discussed in the API Terms of Service.

Posts a bulletin for a specific channel. (The user submitting the request must be authorized to act on the channel's behalf.) Try it now or see an example.

Request

HTTP request

POST https://www.googleapis.com/youtube/v3/activities

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope
https://www.googleapis.com/auth/youtube

Parameters

Parameter name Value Description
Required parameters
part string The part parameter serves two purposes in this operation. It identifies the properties that the write operation will set as well as the properties that the API response will include.

The part names that you can include in the parameter value are snippet and contentDetails.

Request body

Provide a activities resource in the request body.

Response

If successful, this method returns a Activities resource in the response body.

Examples

Note: The code samples below may not represent all supported programming languages. See the client libraries documentation for a list of supported languages.

Python

This example uses the Python client library.

#!/usr/bin/python

import httplib2
import os
import sys

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run
from optparse import OptionParser


# CLIENT_SECRETS_FILE, name of a file containing the OAuth 2.0 information for
# this application, including client_id and client_secret. You can acquire an
# ID/secret pair from the API Access tab on the Google APIs Console
#   http://code.google.com/apis/console#access
# For more information about using OAuth2 to access Google APIs, please visit:
#   https://developers.google.com/accounts/docs/OAuth2
# For more information about the client_secrets.json file format, please visit:
#   https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
# Please ensure that you have enabled the YouTube Data API for your project.
CLIENT_SECRETS_FILE = "client_secrets.json"

# An OAuth 2 access scope that allows for full read/write access.
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

# Helpful message to display if the CLIENT_SECRETS_FILE is missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0

To make this sample run you will need to populate the client_secrets.json file
found at:

   %s

with information from the APIs Console
https://code.google.com/apis/console#access

For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
                                   CLIENT_SECRETS_FILE))

def get_authenticated_service():
  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
    scope=YOUTUBE_READ_WRITE_SCOPE,
    message=MISSING_CLIENT_SECRETS_MESSAGE)

  storage = Storage("%s-oauth2.json" % sys.argv[0])
  credentials = storage.get()

  if credentials is None or credentials.invalid:
    credentials = run(flow, storage)

  return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    http=credentials.authorize(httplib2.Http()))


def post_bulletin(youtube, options):
  body = {}
  if options.message:
    body["snippet"] = dict(description=options.message)
  if options.videoid:
    body["contentDetails"] = dict(
      bulletin=dict(
        resourceId=dict(
          kind="youtube#video",
          videoId=options.videoid
        )
      )
    )
  if options.playlistid:
    body["contentDetails"] = dict(
      bulletin=dict(
        resourceId=dict(
          kind="youtube#playlist",
          playlistId=options.playlistid
        )
      )
    )

  youtube.activities().insert(
    part=",".join(body.keys()),
    body=body
  ).execute()

  print "The bulletin was posted to your channel."


if __name__ == "__main__":
  parser = OptionParser()
  parser.add_option("--message", dest="message", help="Text message to post.") 
  parser.add_option("--videoid", dest="videoid", help="ID of video to post.")
  parser.add_option("--playlistid", dest="playlistid",
    help="ID of playlist to post.")
  (options, args) = parser.parse_args()

  # A bulletin needs to contain at least one of: message, video, playlist.
  # You can post a message with or without an accompanying video or playlist.
  # You can't post both a video and playlist at the same time.

  if options.videoid and options.playlistid:
    exit("You cannot post a video and a playlist at the same time.")

  if not (options.message or options.videoid or options.playlistid):
    exit("Please provide a message, video, or playlist.")

  youtube = get_authenticated_service()
  post_bulletin(youtube, options)

Errors

The table below identifies error messages that the API could return in response to a call to this method.

Error name Reason Likely cause
notFound playlistNotFound YouTube cannot find the video that you are trying to associate with the bulletin post. Check the value of the contentDetails.bulletinPosted.playlistId property.
userRateLimitExceeded rateLimitExceeded The request cannot be completed because you have exceeded your quota.
notFound videoNotFound YouTube cannot find the video that you are trying to associate with the bulletin post. Check the value of the contentDetails.bulletinPosted.videoId property.
badRequest bulletinTextRequired The request must use the snippet object's description property to provide the text for the bulletin post.

Try it!

Use the API Explorer to call this method on live data and see the API request and response.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.