지원 고객 지원 문의 | 시스템 상태 시스템 상태
페이지 내용

    액세스 토큰 얻기

    이 항목에서는 액세스 토큰을 얻는 방법을 배우고 앱에서 이 논리를 구현하기 위한 전략을 보여 줍니다.

    토큰 받기

    API 요청을 테스트하기위한 액세스 토큰을 생성하려는 경우 다음을 사용할 수 있습니다. 샘플 앱 .

    토큰은 Brightcove OAuth API에서 얻습니다. 액세스 토큰을 얻으려면 먼저 액세스하려는 API 및 작업과 관련된 클라이언트 자격 증명 (클라이언트 ID 및 클라이언트 암호)을 얻어야합니다. 클라이언트 자격 증명을 얻으려면 API 자격 증명 관리 .

    자격 증명이 있으면 다음을 수행하여 액세스 토큰을 얻습니다. POST요청 :

      https://oauth.brightcove.com/v4/access_token

    이 호출과 함께 다음 헤더를 전달해야합니다.

    • Content-Type: application/x-www-form-urlencoded
    • Authorization: Basic {client_id}:{client_secret}

    전체{client_id}:{client_secret}문자열은 Base64로 인코딩되어야합니다 (curl은 문자열을 다음과 같이 전달하면 자동으로 Base64로 인코딩됩니다. --user신임장; 다른 언어에서는 Base64 인코딩을 직접 처리해야합니다.)

    또한 다음 키/값 쌍을 요청 본문 또는 URL 매개 변수로 보내야 합니다.

      grant_type=client_credentials

    응답은 다음과 같습니다 (가독성을 위해 여기에 인쇄되어 있습니다).

      {
          "access_token": "ANB7xKhiUZmwltVd3f1odcHHM9VAwg02kwmLwtZwHv3SxGCOWLUf5W4G7X22PRjmR9StvFUqzpVZ1suOfyfOigdi-rnohxyEaSSuZceeLw_9OBW7fXldOG05HEgkeK3N-DBZZZyilodmjA1JWZHbgI3IU7Rmz5IPGyi-sDxHN3KlOr1BDZlLZpXPdFPwEyb6idq-z8AL-blKTSMtNI3_fz3oNBisfrHGUv5tXHoQT4B7FYcvdrap16gTOO7_wNt1zmgLJiUHvyxZgsgBchm_AhohVL-AYgcfCbCR0v7d2hgI4ag35pnZNeujDiBLfnCFcVMlqQGq8UEVZrmU9a8y4pVAGih_EImmghqmSrkxLPYZ800-vIWX-lw",
          "token_type": "Bearer",
          "expires_in": 300
      }

    access_token값은 다음 형식으로 API 호출과 함께Authorization헤더에 전달해야하는 값입니다.

      Authorization: Bearer {access_token}

    그만큼expires_in value는 액세스 토큰이 유효한 시간 (초)입니다.

    구현 전략

    앱이 Brightcove API에 대한 산발적 인 호출 만 수행하는 경우 다음을 무시하는 것이 좋습니다. expires_in매개 변수를 사용하고 모든 호출에 대해 새 액세스 토큰을 가져옵니다. 이 경우 처리 순서는 다음과 같습니다.

    새 토큰 받기
    새 토큰 받기

    반면에 앱이 많은 API 호출을 빠르게 연속적으로 수행한다는 것을 알고 있다면 (예 : 긴 보고서 생성) 필요할 때만 액세스 토큰을 가져 오는 것이 더 효율적입니다. 이를 수행하는 두 가지 기본 방법이 있습니다.

    1. 계속해서 API 호출을 시도하고UNAUTHORIZED오류가 발생하면 새 토큰을 가져 와서 API를 다시 호출하십시오. 이 경우 처리 순서는 다음과 같습니다.
      호출 실패 확인과 함께 토큰 가져 오기
      호출 실패 확인과 함께 토큰 가져 오기
    2. 또 다른 접근 방식은expires_in토큰을 가져올 때마다 Epoch 초 단위의 현재 시간 값으로 설정 한 다음 나중에 API 호출에서 만료 시간을 현재 시간과 비교하여 새 토큰을 가져와야하는지 확인합니다. 이 경우 처리 순서는 다음과 같습니다.
      만료 확인과 함께 토큰 가져 오기
      만료 확인과 함께 토큰 가져 오기

    우편 배달부와 불면증

    REST API에 대한 몇 가지 유용한 테스트 도구는 Brightcove OAuth 시스템과 함께 작동하여 액세스 토큰을 얻도록 설정할 수 있습니다. 가장 널리 사용되는 두 가지 크로스 플랫폼 도구에 대해이 작업을 수행하는 단계가 포함 된 가이드가 있습니다.

    코드 샘플

    다음은 시작하는 데 도움이되는 몇 가지 코드 샘플입니다.

    쉘 스크립트 예

    첫 번째 예는 위의 첫 번째 구현 논리를 구현하는 셸 스크립트입니다. 사용자로부터 입력을 받고 항상 새 토큰을 가져온 다음 API를 호출합니다. 스크립트는 모든 Brightcove API에서 작동하며 앱을 빌드 할 때 API 호출을 테스트하는 데 유용 할 수 있습니다.

    쉘 스크립트 코드

      bold=`tput bold`
      normal=`tput sgr0`
      echo 'Enter your client id:'
      read CLIENT_ID
      echo Your client id: $CLIENT_ID
      echo --------------------------
      echo 'Enter your client secret:'
      read CLIENT_SECRET
      echo Your client secret: $CLIENT_SECRET
      echo --------------------------
      echo 'Enter the full API call:'
      read API_CALL
      echo Your API call: $API_CALL
      echo --------------------------
      echo "Enter the HTTP method: [ ${bold}g${normal} (GET - default) | ${bold}po${normal} (POST) | ${bold}pa${normal} (PATCH) | ${bold}pu${normal} (PUT) | ${bold}d${normal} (DELETE) ]:"
      read VERB
      if [ "$VERB" = "" ]
          then
          export VERB="GET"
      elif [ "$VERB" = "g" ] || [ "$VERB" = "GET" ] || [ "$VERB" = "get" ]
          then
          export VERB="GET"
      elif [ "$VERB" = "po" ] || [ "$VERB" = "p" ] || [ "$VERB" = "POST" ] || [ "$VERB" = "post" ]
          then
          export VERB="POST"
      elif [ "$VERB" = "pa" ] || [ "$VERB" = "PATCH" ] || [ "$VERB" = "patch" ]
          then
          export VERB="PATCH"
      elif [ "$VERB" = "pu" ] || [ "$VERB" = "PUT" ] || [ "$VERB" = "put" ]
          then
          export VERB="PUT"
      elif [ "$VERB" = "d" ] || [ "$VERB" = "DELETE" ] || [ "$VERB" = "delete" ]
          then
          export VERB="DELETE"
      fi
      echo "Your request type: $VERB"
      echo --------------------------
      echo 'Enter data to be submitted in the request body:'
      read DATA
      echo Your call verb: $DATA
      echo --------------------------
      # get access token and use regex to extract it from the response
      TOKEN=$(curl -s --data "grant_type=client_credentials" https://oauth.brightcove.com/v4/access_token --header "Content-Type: application/x-www-form-urlencoded" --user "$CLIENT_ID:$CLIENT_SECRET" | sed -E 's/.*access_token\"\:\"([^\"]+)\".*/\1/');
      echo Your token: $TOKEN
      echo --------------------------
      RESPONSE=$(curl -s -v -X $VERB "$API_CALL" -d "$DATA" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json")
      echo Raw response:
      echo $RESPONSE
      echo --------------------------
      echo Pretty-printed response:
      echo $RESPONSE | python -m json.tool
      echo
      

    Ruby 예제

    다음 예제는 첫 번째 구현 로직도 사용하는 Ruby 스크립트입니다. 항상 토큰을 얻은 다음 API를 호출합니다. 이 예에서는 Analytics API를 호출하지만 모든 API와 함께 작동하도록 조정할 수 있습니다.

    루비 코드

      #!/usr/bin/env ruby
      # view id --> content,
      #!/usr/bin/env ruby
      require 'rest-client'
      require 'json'
      client_id = '5eb0f20e-29a8-4f19-8cb5-80336e2789ab'
      client_secret = 'Zqpb_2YrvnGUEjqQUndx6GsjQ3JyAgXoA2gNbhoj-yUV4scij0jwCN0OBz9FILEwHupjeqwdbOUSFMi7zkhpVg'
      response = RestClient.post 'https://oauth.brightcove.com/v4/access_token', :client_id=>client_id,:client_secret=>client_secret,:grant_type=>'client_credentials'
      token = JSON.parse(response)["access_token"]
      puts "The extracted token is:" + token + "\n\n\n"
      data = RestClient.get 'https://data.brightcove.com/analytics-api/videocloud/account/1234567890001/report?dimensions=video&from=2014-01-01&to=2014-03-30', { 'Authorization' => "Bearer #{token}", 'Accept' => 'application/json' }
      puts "This is the result from the query: \n" + data
      

    Python 예

    이 샘플은 위의 세 번째 구현 로직을 구현하는 Python 스크립트입니다. 그것은 만들려고 시도합니다Analytics API호출하지만 UNAUTHORIZED 오류로 호출이 실패하면 새 액세스 토큰을 가져와 호출을 재 시도합니다.

    이 스크립트는 또한 외부 파일에서 클라이언트 자격 증명을 읽습니다. 자격 증명 데이터 파일은 Python 코드 아래에 표시됩니다.

    파이썬 코드

      import httplib, urllib, base64, json, sys
      # This is a python script to test the CMS API.
      # To use this script, edit the configuration file brightcove_oauth.txt
      # with your brightcove account ID, and a client ID and client secret for
      # an Oauth credential that has CMS API - Videos Read permission.
      # You can find instructions on how to generate Oauth credentials
      # https://apis.support.brightcove.com/cms/getting-started/practice-area-cms-api.html
      # This script demonstrates how to refresh the access token
      # in handling 401 - Unauthorized errors from the CMS API
      # Because the Oauth tokens have a 300 second time to live,
      # The refresh logic to handle 401 errors will be a normal part of runtime behavior.
      # Note that the client_id and client_secret secure the access to the CMS API
      # Therefore, it is not advisable to expose them to browsers. These are meant for
      # server to server communication to obtain an access token.
      # The access token can be exposed to the browser. Its limited permissions and expiry
      # time make limit the duration and scope of its usage should it be observed in network
      # traffic or obtained from a browser.
      class AuthError(Exception):
      def __init__(self):
      self.msg = "auth error"
      # read the oauth secrets and account ID from a configuration file
      def loadSecret():
      # read the s3 creds from json file
      try:
              credsFile=open('brightcove_oauth.txt')
              creds = json.load(credsFile)
      return creds
      except Exception, e:
      print "Error loading oauth secret from local file called 'brightcove_oauth.txt'"
      print "\tThere should be a local file in this directory called brightcove_oauth.txt "
      print "\tWhich has contents like this:"
      print """
    
          {
              "account_id": "1234567890001",
          "client_id": "30ff0909-0909-33d3-ae88-c9887777a7b7",
          "client_secret": "mzKKjZZyeW5YgsdfBD37c5730g397agU35-Dsgeox6-73giehbeihgleh659dhgjhdegessDge0s0ynegg987t0996nQ"
          }
    
          """
          sys.exit("System error: " + str(e) );
      # get the oauth 2.0 token
      def getAuthToken(creds):
          conn = httplib.HTTPSConnection("oauth.brightcove.com")
          url =  "/v4/access_token"
          params = {
      "grant_type": "client_credentials"
          }
          client = creds["client_id"];
          client_secret = creds["client_secret"];
          authString = base64.encodestring('%s:%s' % (client, client_secret)).replace('\n', '')
          requestUrl = url + "?" + urllib.urlencode(params)
          headersMap = {
      "Content-Type": "application/x-www-form-urlencoded",
      "Authorization": "Basic " + authString
          };
          conn.request("POST", requestUrl, headers=headersMap)
          response = conn.getresponse()
      if response.status == 200:
              data = response.read()
              result = json.loads( data )
      return result["access_token"]
      # call Analytics API for video views in the last 30 days
      def getVideoViews( token , account ):
          conn = httplib.HTTPSConnection("data.brightcove.com")
          url =  "/analytics-api/videocloud/account/" + account + "/report/"
          params = {
      "dimensions": "video",
      "limit": "10",
      "sort": "video_view",
      "fields": "video,video_name,video_view",
      "format": "json"
          }
          requestUrl = url + "?" + urllib.urlencode(params)
          headersMap = {
      "Authorization": "Bearer " + token
          };
          conn.request("POST", requestUrl, headers=headersMap)
          response = conn.getresponse()
      if response.status == 200:
              data = response.read()
              result = json.loads( data )
      return result
      elif response.status == 401:
      # if we get a 401 it is most likely because the token is expired.
      raise AuthError
      else:
      raise Exception('API_CALL_ERROR' + " error " + str(response.status) )
      # call CMS API to return the number of videos in the catalog
      def getVideos( token , account ):
          conn = httplib.HTTPSConnection("cms.api.brightcove.com")
          url =  "/v1/accounts/" + account + "/counts/videos/"
          requestUrl = url
      print "GET " + requestUrl
          headersMap = {
      "Authorization": "Bearer " + token
          };
          conn.request("GET", requestUrl, headers=headersMap)
          response = conn.getresponse()
      if response.status == 200:
              data = response.read()
              result = json.loads( data )
      return result
      elif response.status == 401:
      # if we get a 401 it is most likely because the token is expired.
      raise AuthError
      else:
      raise Exception('API_CALL_ERROR' + " error " + str(response.status) )
      def demo():
          creds = loadSecret()
          token = getAuthToken(creds)
          account = creds["account"];
      try:
              results = getVideos( token , account )
      except AuthError, e:
      # handle an auth error by re-fetching a auth token again
              token = getAuthToken(creds)
              results = getVideoViews( token , account )
      # print the videos
      print results
      if __name__ == "__main__":
        demo();
      
    Python 샘플 용 자격 증명 파일
      {
          "account" : "1234567890001",
          "client_id": "30ff0909-0909-33d3-ae88-c9887777a7b7",
          "client_secret": "XXXXXXXX_XXXXXXXX_XXXXXXXX_XXXXXXX_XXXXXXX_XXXXXXXXXXXXXXXXX_XXXXXXXXXXX"
      }

    PHP 예

    이것은 클라이언트 자격 증명과 API 호출을 받고, 액세스 토큰을 받고, API 요청을하고, 결과를 클라이언트에 반환하는 간단한 프록시입니다.

    PHP 코드

      <?php
      /**
       * proxy for Brightcove RESTful APIs
       * gets an access token, makes the request, and returns the response
       *
       * Method: POST
       * include header: "Content-Type", "application/x-www-form-urlencoded"
       *
       * @post {string} url - the URL for the API request
       * @post {string} [requestType=GET] - HTTP method for the request
       * @post {string} [requestBody=null] - JSON data to be sent with write requests
       *
       * @returns {string} $response - JSON response received from the API
       */
      // CORS enablement
      header("Access-Control-Allow-Origin: *");
      // set up request for access token
      $data = array();
      $client_id     = ‘YOUR_CLIENT_ID’;
      $client_secret = ‘YOUR_CLIENT_SECRET’;
      $auth_string   = "{$client_id}:{$client_secret}";
      $request       = "https://oauth.brightcove.com/v4/access_token?grant_type=client_credentials";
      $ch            = curl_init($request);
      curl_setopt_array($ch, array(
        CURLOPT_POST           => TRUE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_USERPWD        => $auth_string,
        CURLOPT_HTTPHEADER     => array(
          'Content-type: application/x-www-form-urlencoded',
        ),
        CURLOPT_POSTFIELDS => $data
      ));
      $response = curl_exec($ch);
      curl_close($ch);
      // Check for errors
      if ($response === FALSE) {
        die(curl_error($ch));
      }
      // Decode the response
      $responseData = json_decode($response, TRUE);
      $access_token = $responseData["access_token"];
      // set up the API call
      // get data
      if ($_POST["requestBody"]) {
        $data = json_decode($_POST["requestBody"]);
      } else {
        $data = array();
      }
      // get request type or default to GET
      if ($_POST["requestType"]) {
        $method = $_POST["requestType"];
      } else {
        $method = "GET";
      }
      // get the URL and authorization info from the form data
      $request = $_POST["url"];
      //send the http request
      $ch = curl_init($request);
      curl_setopt_array($ch, array(
          CURLOPT_CUSTOMREQUEST  => $method,
          CURLOPT_RETURNTRANSFER => TRUE,
          CURLOPT_SSL_VERIFYPEER => FALSE,
          CURLOPT_HTTPHEADER     => array(
            'Content-type: application/json',
            "Authorization: Bearer {$access_token}",
          ),
          CURLOPT_POSTFIELDS => json_encode($data)
        ));
      $response = curl_exec($ch);
      curl_close($ch);
      // Check for errors
      if ($response === FALSE) {
        $logEntry = "\nError:\n".
        "\n".date("Y-m-d H:i:s")." UTC \n"
        .$response;
        $logFileLocation = "log.txt";
        $fileHandle      = fopen($logFileLocation, 'a') or die("-1");
        fwrite($fileHandle, $logEntry);
        fclose($fileHandle);
        echo "Error: there was a problem with your API call"+
        die(curl_error($ch));
      }
      // Decode the response
      // $responseData = json_decode($response, TRUE);
      // return the response to the AJAX caller
      echo $response;
      ?>
      

    Powershell 샘플

      $ParentPath = "C:\Temp"
      $ParentCsv = "$ParentPath\Videos.csv"
    
    
    
      Clear-Host
    
    
    
      <#
          .SYNOPSIS
              Retrieves the TokenType and AccessToken from Brightcove.
    
          .DESCRIPTION
              Uses the Brightcove API to retrieve TokenType and AccessToken for use in later
              API requests. The AccessToken expires after 300 seconds (5 minutes) and a new
              AccessToken will need to be requested.
      #>
      function Get-BrightcoveAuthorization
      {
          # /oauth/getting-started/overview-oauth-api-v4.html
    
          $Uri = "https://oauth.brightcove.com/v4/access_token"
    
          $ClientId = "" # <--------------------------------------------------------------------Retrieve from Brightcove and paste here
          $ClientSecret = "" # <----------------------------------------------------------------Retrieve from Brightcove and paste here
          $Authorization = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($ClientId + ":" + $ClientSecret))
    
          $Headers = @{
            "Authorization" = "Basic " + $Authorization;
            "Content-Type" = "application/x-www-form-urlencoded";
          }
    
          Invoke-RestMethod -Method "Post" -Uri $Uri -Body "grant_type=client_credentials" -Headers $Headers
      }
    
    
    
      <#
          .SYNOPSIS
              Retrieves a count of videos available for a Brightcove Video Cloud account.
    
          .DESCRIPTION
              Uses the Brightcove API to retrieve the count of videos.
    
          .PARAMETER TokenType
              Required. The token type as retrieved from Brightcove's authorization API.
    
          .PARAMETER AccessToken
              Required. The access toke as retrieved from Brightcove's authorization API.
      #>
      function Get-BrightcoveVideoCount
      {
          # https://support.brightcove.com/getting-counts-videos-and-playlists
    
          param(
              [parameter(Mandatory=$true)]
              [string]
              $TokenType,
    
              [parameter(Mandatory=$true)]
              [string]
              $AccessToken
          )
    
          $Uri = "https://cms.api.brightcove.com/v1/accounts/1044238710001/counts/videos"
    
          $Headers = @{
            "Authorization" = "$TokenType $AccessToken";
          }
    
          (Invoke-RestMethod -Method "Get" -Uri $Uri -Headers $Headers).count
      }
    
    
    
      <#
          .SYNOPSIS
              Retrieves a list of videos available for a Brightcove Video Cloud account.
    
          .DESCRIPTION
              Uses the Brightcove API to retrieve the information for a list of videos, paged
              up to a specified Limit and starting ad a specified Offset.
    
          .PARAMETER TokenType
              Required. The token type as retrieved from Brightcove's authorization API.
    
          .PARAMETER AccessToken
              Required. The access toke as retrieved from Brightcove's authorization API.
    
          .PARAMETER Limit
              Optional. Number of videos to return - must be an integer between 1 and 100.
              Default: 20
    
          .PARAMETER Offset
              Optional. Number of videos to skip (for paging results). Must be a positive integer.
              Default: 0
      #>
      function Get-BrightcoveVideos
      {
          # https://support.brightcove.com/overview-cms-api
          # https://support.brightcove.com/using-cms-api-retrieve-video-data#bc-ipnav-1
          # https://support.brightcove.com/cmsplayback-api-videos-search
    
          param(
              [parameter(Mandatory=$true)]
              [string]
              $TokenType,
    
              [parameter(Mandatory=$true)]
              [string]
              $AccessToken,
    
              [ValidateRange(1, 100)]
              [int]
              $Limit = 20,
    
              [ValidateRange(0, [int]::MaxValue)]
              [int]
              $Offset = 0
          )
    
          $Uri = "https://cms.api.brightcove.com/v1/accounts/1044238710001/videos"
    
          if ($Limit)
          {
              $Uri += "?limit=$Limit"
          }
    
          if ($Offset -and $Offset -ne 0)
          {
              if ($Limit)
              {
                  $Uri += "&offset=$Offset"
              }
              else
              {
                  $Uri += "?offset=$Offset"
              }
          }
    
          $Headers = @{
            "Authorization" = "$TokenType $AccessToken";
          }
    
          Invoke-RestMethod -Method "Get" -Uri $Uri -Headers $Headers
      }
    
    
    
      <#
          .SYNOPSIS
              Retrieves a list of sources available for a Brightcove video.
    
          .DESCRIPTION
              Uses the Brightcove API to retrieve the list of video file sources for a
              specific video.
    
          .PARAMETER TokenType
              Required. The token type as retrieved from Brightcove's authorization API.
    
          .PARAMETER AccessToken
              Required. The access toke as retrieved from Brightcove's authorization API.
    
          .PARAMETER VideoId
              Required. ID of the video to get information for. This can be obtained using
              the Get-BrightcoveVideos function or Brightcove's website.
      #>
      function Get-BrightcoveVideoSources
      {
          # https://support.brightcove.com/using-cms-api-retrieve-video-data#bc-ipnav-3
    
          param(
              [parameter(Mandatory=$true)]
              [string]
              $TokenType,
    
              [parameter(Mandatory=$true)]
              [string]
              $AccessToken,
    
              [parameter(Mandatory=$true)]
              [string]
              $VideoId
          )
    
          $Uri = "https://cms.api.brightcove.com/v1/accounts/1044238710001/videos/$VideoId/sources"
    
          $Headers = @{
            "Authorization" = "$TokenType $AccessToken";
          }
    
          Invoke-RestMethod -Method "Get" -Uri $Uri -Headers $Headers
      }
    
    
    
      <#
          .SYNOPSIS
              Retrieves a list of images associated with a Brightcove video.
    
          .DESCRIPTION
              Uses the Brightcove API to retrieve the information of the thumbnail and poster
              for a specific video.
    
          .PARAMETER TokenType
              Required. The token type as retrieved from Brightcove's authorization API.
    
          .PARAMETER AccessToken
              Required. The access toke as retrieved from Brightcove's authorization API.
    
          .PARAMETER VideoId
              Required. ID of the video to get information for. This can be obtained using
              the Get-BrightcoveVideos function or Brightcove's website.
      #>
      function Get-BrightcoveVideoImages
      {
          # https://support.brightcove.com/using-cms-api-retrieve-video-data#bc-ipnav-4
    
          param(
              [parameter(Mandatory=$true)]
              [string]
              $TokenType,
    
              [parameter(Mandatory=$true)]
              [string]
              $AccessToken,
    
              [parameter(Mandatory=$true)]
              [string]
              $VideoId
          )
    
          $Uri = "https://cms.api.brightcove.com/v1/accounts/1044238710001/videos/$VideoId/images"
    
          $Headers = @{
            "Authorization" = "$TokenType $AccessToken";
          }
    
          Invoke-RestMethod -Method "Get" -Uri $Uri -Headers $Headers
      }
    
    
    
      <#
          .SYNOPSIS
              Downloads a file from the web.
    
          .DESCRIPTION
              Uses the BITS to retrieve a file from a given URI.
    
          .PARAMETER Path
              Required. The folder path to save the file to. The filename will be determined
              by the URI.
    
          .PARAMETER Uri
              Required. The URI for the location of the file on the web. This will be used to
              determine the filename of the file.
    
          .PARAMETER DisplayName
              Optional. This is what will be displayed at the top of the progress bar.
      #>
      function Start-BrightcoveDownload
      {
          param(
              [parameter(Mandatory=$true)]
              [string]
              $Path,
    
              [parameter(Mandatory=$true)]
              [string]
              $Uri,
    
              [string]
              $DisplayName
          )
    
          $FileName = (($Uri -split "/")[-1] -split "\?")[0]
    
          if ([string]::IsNullOrWhiteSpace($DisplayName))
          {
              $DisplayName = "Downloading file..."
          }
    
          Start-BitsTransfer -Source $Uri -Destination "$Path\$FileName" -DisplayName $DisplayName -Description $FileName
      }
    
    
    
      <#
          .SYNOPSIS
              Replaces invalid characters from a filename.
    
          .DESCRIPTION
              Replaces the invalid characters in a filename with an underscore (_).
    
          .PARAMETER Name
              Required. Filename to have the invalid characters removed from.
      #>
      function Replace-InvalidFileNameChars {
          param(
              [Parameter(Mandatory=$true)]
              [String]$Name
          )
    
          $InvalidFileNameChars = [IO.Path]::GetInvalidFileNameChars() -join ''
          $Replace = "[{0}]" -f [RegEx]::Escape($InvalidFileNameChars)
    
          return ($Name -replace $Replace, "_")
      }
    
    
    
    
    
    
    
      # Get AccessToken for API
      "Getting AccessToken for API..."
      $BrightcoveAuthorization = Get-BrightcoveAuthorization
    
      $AccessToken = $BrightcoveAuthorization.access_token
      $AccessTokenExpiresIn = $BrightcoveAuthorization.expires_in #seconds (300)
      $TokenType = $BrightcoveAuthorization.token_type
    
      $AccessTokenExpiry = (Get-Date) + (New-TimeSpan -Seconds $AccessTokenExpiresIn)
    
    
    
      # Get count of available videos
      "Getting count of available videos..."
      $BrightcoveVideoCount = Get-BrightcoveVideoCount -AccessToken $AccessToken -TokenType $TokenType
    
    
    
      # Get list of all videos 20 at a time
      "Getting list of all videos..."
      $BrightcoveVideos = @()
      for ($i = 0; $i -lt $BrightcoveVideoCount; $i += 20) {
          $BrightcoveVideos += Get-BrightcoveVideos -AccessToken $AccessToken -TokenType $TokenType -Offset $i
      }
    
    
    
      # Parse videos and download information, video, and thumbnail files
      "Parsing videos and downloading information, video, and thumbnail files..."
      foreach ($BrightcoveVideo in $BrightcoveVideos)
      {
          $Thumbnail = ""
          $Poster = ""
    
          $Video = [pscustomobject][ordered]@{
              Id = $BrightcoveVideo.id
              Complete = $BrightcoveVideo.complete
              CreatedAt = $BrightcoveVideo.created_at
              Duration = $BrightcoveVideo.duration
              Name = $BrightcoveVideo.name
              OriginalFileName = $BrightcoveVideo.original_filename
              PublishedAt = $BrightcoveVideo.published_at
              State = $BrightcoveVideo.state
              Tags = $BrightcoveVideo.tags -join ","
              UpdatedAt = $BrightcoveVideo.updated_at
          }
    
          $VideoName = $Video.Name
          $PathFriendlyVideoName = Replace-InvalidFileNameChars -Name $VideoName
    
          $Path = "$ParentPath\$PathFriendlyVideoName"
    
          # Get new AccessToken if expired
          if ((Get-Date) -gt $AccessTokenExpiry)
          {
              $BrightcoveAuthorization = Get-BrightcoveAuthorization
    
              $AccessToken = $BrightcoveAuthorization.access_token
              $AccessTokenExpiresIn = $BrightcoveAuthorization.expires_in #seconds (300)
              $TokenType = $BrightcoveAuthorization.token_type
    
              $AccessTokenExpiry = (Get-Date) + (New-TimeSpan -Seconds $AccessTokenExpiresIn)
          }
    
          # Get list of rendition sources for video and select last MP4, sorted by width
          $BrightcoveVideoSources = Get-BrightcoveVideoSources -AccessToken $AccessToken -TokenType $TokenType -VideoId $Video.Id
          $Source = $BrightcoveVideoSources | where -Property "container" -EQ -Value "MP4" | sort -Property width | select -Last 1
          $SourceUri = $Source.src
    
          # Get list of images for video
          $BrightcoveVideoImages = Get-BrightcoveVideoImages -AccessToken $AccessToken -TokenType $TokenType -VideoId $Video.Id
          $Thumbnail = $BrightcoveVideoImages.thumbnail
          $ThumbnailUri = $Thumbnail.src
          $Poster = $BrightcoveVideoImages.poster
          $PosterUri = $Poster.src
    
          # Create video download folder
          if (-not (Test-Path $Path))
          {
              New-Item -Path $Path -ItemType Directory |
                  Out-Null
          }
    
          # Append video information to parent CSV
          $Video |
              Export-Csv -Path $ParentCsv -NoTypeInformation -Append
    
          # Write video inforamtion to video CSV
          $Video |
              Export-Csv -Path "$Path\$PathFriendlyVideoName.csv" -NoTypeInformation
    
          # Download video thumbnail
          if (-not [string]::IsNullOrWhiteSpace($ThumbnailUri))
          {
              Start-BrightcoveDownload -Path $Path -Uri $ThumbnailUri -DisplayName "Downloading thumbnail for $VideoName"
          }
    
          # Download video poster
          if (-not [string]::IsNullOrWhiteSpace($PosterUri))
          {
              Start-BrightcoveDownload -Path $Path -Uri $PosterUri -DisplayName "Downloading poster for $VideoName"
          }
    
          # Download video file
          if (-not [string]::IsNullOrWhiteSpace($PosterUri))
          {
              Start-BrightcoveDownload -Path $Path -Uri $SourceUri -DisplayName "Downloading video for $VideoName"
          }
      }
    
    
    
      "\n"
      "Finished downloading files. Look for the list of videos in a CSV file at the root of "
      "the parent path. Each video is downloaded to its own separate folder along with its "
      "own CSV and image files."
      Explorer.exe $ParentPath