Share via


Quickstart: Use the Bing Video Search client library

Warning

On October 30, 2020, the Bing Search APIs moved from Azure AI services to Bing Search Services. This documentation is provided for reference only. For updated documentation, see the Bing search API documentation. For instructions on creating new Azure resources for Bing search, see Create a Bing Search resource through the Azure Marketplace.

Use this quickstart to begin searching for news with the Bing Video Search client library for C#. While Bing Video Search has a REST API compatible with most programming languages, the client library provides an easy way to integrate the service into your applications. The source code for this sample can be found on GitHub with additional annotations, and features.

Prerequisites

To add the Bing Video Search client library to your project, select Manage NuGet Packages from Solution Explorer in Visual Studio. Add the Microsoft.Azure.CognitiveServices.Search.VideoSearch package.

Installing the [NuGet Video Search SDK package] also installs the following dependencies:

  • Microsoft.Rest.ClientRuntime
  • Microsoft.Rest.ClientRuntime.Azure
  • Newtonsoft.Json

Create an Azure resource

Start using the Bing Video Search API by creating one of the following Azure resources:

Bing Search v7 resource

  • Available through the Azure portal until you delete the resource.
  • Use the free pricing tier to try the service, and upgrade later to a paid tier for production.

Multi-service resource

  • Available through the Azure portal until you delete the resource.
  • Use the same key and endpoint for your applications, across multiple Azure AI services.

Create and initialize a project

  1. Create a new C# console solution in Visual Studio. Then add the following into the main code file.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Azure.CognitiveServices.Search.VideoSearch;
    using Microsoft.Azure.CognitiveServices.Search.VideoSearch.Models;
    
  2. Instantiate the client by creating a new ApiKeyServiceClientCredentials object with your subscription key, and calling the constructor.

    var client = new VideoSearchAPI(new ApiKeyServiceClientCredentials("YOUR-ACCESS-KEY"));
    

Send a search request and process the results

  1. Use the client to send a search request. Use "SwiftKey" for the search query.

    var videoResults = client.Videos.SearchAsync(query: "SwiftKey").Result;
    
  2. If any results were returned, get the first one with videoResults.Value[0]. Then print the video's ID, title, and url.

    if (videoResults.Value.Count > 0)
    {
        var firstVideoResult = videoResults.Value[0];
    
        Console.WriteLine($"\r\nVideo result count: {videoResults.Value.Count}");
        Console.WriteLine($"First video id: {firstVideoResult.VideoId}");
        Console.WriteLine($"First video name: {firstVideoResult.Name}");
        Console.WriteLine($"First video url: {firstVideoResult.ContentUrl}");
    }
    else
    {
        Console.WriteLine("Couldn't find video results!");
    }
    

Next steps

See also

Use this quickstart to begin searching for news with the Bing Video Search client library for Java. While Bing Video Search has a REST API compatible with most programming languages, the client library provides an easy way to integrate the service into your applications. The source code for this sample can be found on GitHub, with additional annotations, and features.

Prerequisites

Create an Azure resource

Start using the Bing Video Search API by creating one of the following Azure resources:

Bing Search v7 resource

  • Available through the Azure portal until you delete the resource.
  • Use the free pricing tier to try the service, and upgrade later to a paid tier for production.

Multi-service resource

  • Available through the Azure portal until you delete the resource.
  • Use the same key and endpoint for your applications, across multiple Azure AI services.

Install the Bing Video Search client library dependencies by using Maven, Gradle, or another dependency management system. The Maven POM file requires the following declaration:

  <dependencies>
    <dependency>
      <groupId>com.microsoft.azure.cognitiveservices</groupId>
      <artifactId>azure-cognitiveservices-videosearch</artifactId>
      <version>0.0.1-beta-SNAPSHOT</version>
    </dependency>
  </dependencies> 

Create and initialize a project

Create a new Java project in your favorite IDE or editor, and import the following libraries.

    import com.microsoft.azure.cognitiveservices.videosearch.*;
    import com.microsoft.azure.cognitiveservices.videosearch.VideoObject;
    import com.microsoft.rest.credentials.ServiceClientCredentials;
    import okhttp3.Interceptor;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List; 

Create a search client

  1. Implement the VideoSearchAPIImpl client, which requires your API endpoint, and an instance of the ServiceClientCredentials class.

    public static VideoSearchAPIImpl getClient(final String subscriptionKey) {
        return new VideoSearchAPIImpl("https://api.cognitive.microsoft.com/bing/v7.0/",
                new ServiceClientCredentials() {
                //...
                }
    )};
    

    To implement ServiceClientCredentials, follow these steps:

    1. override the applyCredentialsFilter() function, with a OkHttpClient.Builder object as a parameter.

      //...
      new ServiceClientCredentials() {
              @Override
              public void applyCredentialsFilter(OkHttpClient.Builder builder) {
              //...
              }
      //...
      
    2. Within applyCredentialsFilter(), call builder.addNetworkInterceptor(). Create a new Interceptor object, and override its intercept() method to take a Chain interceptor object.

      //...
      builder.addNetworkInterceptor(
          new Interceptor() {
              @Override
              public Response intercept(Chain chain) throws IOException {
              //...    
              }
          });
      ///...
      
    3. Within the intercept function, create variables for your request. Use Request.Builder() to build your request. Add your subscription key to the Ocp-Apim-Subscription-Key header, and return chain.proceed() on the request object.

      //...
      public Response intercept(Chain chain) throws IOException {
          Request request = null;
          Request original = chain.request();
          Request.Builder requestBuilder = original.newBuilder()
                  .addHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
          request = requestBuilder.build();
          return chain.proceed(request);
      }
      //...
      

Send a search request and receive the response

  1. Create a function called VideoSearch() that takes your subscription key as a string. Instantiate the search client created earlier.

    public static void VideoSearch(String subscriptionKey){
        VideoSearchAPIImpl client = VideoSDK.getClient(subscriptionKey);
        //...
    }
    
  2. Within VideoSearch(), Send a video search request using the client, with SwiftKey as the search term. If the Video Search API returned a result, get the first result and print its id, name, and URL, along with the total number of videos returned.

    VideosInner videoResults = client.searchs().list("SwiftKey");
    
    if (videoResults == null){
        System.out.println("Didn't see any video result data..");
    }
    else{
        if (videoResults.value().size() > 0){
            VideoObject firstVideoResult = videoResults.value().get(0);
    
            System.out.println(String.format("Video result count: %d", videoResults.value().size()));
            System.out.println(String.format("First video id: %s", firstVideoResult.videoId()));
            System.out.println(String.format("First video name: %s", firstVideoResult.name()));
            System.out.println(String.format("First video url: %s", firstVideoResult.contentUrl()));
        }
        else{
            System.out.println("Couldn't find video results!");
        }
    }
    
  3. Call the search method from your main method.

    public static void main(String[] args) {
        VideoSDK.VideoSearch("YOUR-SUBSCRIPTION-KEY");
    }
    

Next steps

See also

Use this quickstart to begin searching for news with the Bing Video Search client library for JavaScript. While Bing Video Search has a REST API compatible with most programming languages, the client library provides an easy way to integrate the service into your applications. The source code for this sample can be found on GitHub. It contains more annotations and features.

Prerequisites

  • The latest version of Node.js.
  • The Bing Video Search SDK for JavaScript
    • To install, run npm install @azure/cognitiveservices-videosearch
  • The CognitiveServicesCredentials class from @azure/ms-rest-azure-js package to authenticate the client.
    • To install, run npm install @azure/ms-rest-azure-js

Create an Azure resource

Start using the Bing Video Search API by creating one of the following Azure resources:

Bing Search v7 resource

  • Available through the Azure portal until you delete the resource.
  • Use the free pricing tier to try the service, and upgrade later to a paid tier for production.

Multi-service resource

  • Available through the Azure portal until you delete the resource.
  • Use the same key and endpoint for your applications, across multiple Azure AI services.

Create and initialize the application

  1. Create a new JavaScript file in your favorite IDE or editor, and add a require() statement for the Bing Video Search client library, and CognitiveServicesCredentials module. Create a variable for your subscription key.

    const CognitiveServicesCredentials = require('@azure/ms-rest-azure-js').CognitiveServicesCredentials;
    const VideoSearchAPIClient = require('@azure/cognitiveservices-videosearch');
    
  2. Create an instance of CognitiveServicesCredentials with your key. Then use it to create an instance of the video search client.

    let credentials = new CognitiveServicesCredentials('YOUR-ACCESS-KEY');
    let client = new VideoSearchAPIClient(credentials);
    

Send the search request

  1. Use client.videosOperations.search() to send a search request to the Bing Video Search API. When the search results are returned, use .then() to log the result.

    client.videosOperations.search('Interstellar Trailer').then((result) => {
        console.log(result.value);
    }).catch((err) => {
        throw err;
    });
    

Next steps

See also

Use this quickstart to begin searching for news with the Bing Video Search client library for Python. While Bing Video Search has a REST API compatible with most programming languages, the client library provides an easy way to integrate the service into your applications. The source code for this sample can be found on GitHub with additional annotations, and features.

Create an Azure resource

Start using the Bing Video Search API by creating one of the following Azure resources:

Bing Search v7 resource

  • Available through the Azure portal until you delete the resource.
  • Use the free pricing tier to try the service, and upgrade later to a paid tier for production.

Multi-service resource

  • Available through the Azure portal until you delete the resource.
  • Use the same key and endpoint for your applications, across multiple Azure AI services.

Prerequisites

  • Python 2.x or 3.x
  • The Bing Video Search client library for python

It is recommended that you use a Python virtual environment. You can install and initialize a virtual environment with the venv module. Install virtualenv for Python 2.7 with:

python -m venv mytestenv

Install the Bing Video Search client library with:

cd mytestenv
python -m pip install azure-cognitiveservices-search-videosearch

Create and initialize the application

  1. Create a new Python file in your favorite IDE or editor, and add the following import statements.

    from azure.cognitiveservices.search.videosearch import VideoSearchClient
    from azure.cognitiveservices.search.videosearch.models import VideoPricing, VideoLength, VideoResolution, VideoInsightModule
    from msrest.authentication import CognitiveServicesCredentials
    
  2. Create a variable for your subscription key.

    subscription_key = "YOUR-SUBSCRIPTION-KEY"
    endpoint = "YOUR-ENDPOINT"
    

Create the search client

Create an instance of the CognitiveServicesCredentials, and instantiate the client:

client = VideoSearchAPI(endpoint, CognitiveServicesCredentials(subscription_key))

Send a search request and get a response

  1. Use client.videos.search() with your search query to send a request to the Bing Video Search API, and get a response.

    video_result = client.videos.search(query="SwiftKey")
    
  2. If the response contains search results, get the first one, and print its ID, name, and url.

    if video_result.value:
        first_video_result = video_result.value[0]
        print("Video result count: {}".format(len(video_result.value)))
        print("First video id: {}".format(first_video_result.video_id))
        print("First video name: {}".format(first_video_result.name))
        print("First video url: {}".format(first_video_result.content_url))
    else:
        print("Didn't see any video result data..")
    

Next steps

See also