快速入门:使用必应视频搜索客户端库
警告
2020 年 10 月 30 日,必应搜索 API 从 Azure AI 服务迁移到必应搜索服务。 本文档仅供参考。 有关更新的文档,请参阅必应搜索 API 文档。 关于为必应搜索创建新的 Azure 资源的说明,请参阅通过 Azure 市场创建必应搜索资源。
通过本快速入门开始使用适用于 C# 的必应视频搜索客户端库来搜索新闻。 虽然必应视频搜索具有与大多数编程语言兼容的 REST API,但该客户端库提供了一种简单方法来将服务集成到应用程序中。 此示例的源代码可以在 GitHub 上找到,其中包含其他注释和功能。
先决条件
- 任何版本的 Visual Studio 2017 或更高版本。
- Json.NET 框架,以 NuGet 程序包的形式提供。
若要向项目中添加必应视频搜索客户端库,请在 Visual Studio 中的解决方案资源管理器中选择“管理 NuGet 包”选项。 添加 Microsoft.Azure.CognitiveServices.Search.VideoSearch
程序包。
安装 [NuGet 视频搜索 SDK 程序包]还将安装以下依赖项:
- Microsoft.Rest.ClientRuntime
- Microsoft.Rest.ClientRuntime.Azure
- Newtonsoft.Json
创建 Azure 资源
通过创建以下 Azure 资源之一开始使用必应视频搜索 API:
- 在删除资源前,可通过 Azure 门户使用。
- 使用免费定价层试用该服务,稍后升级到用于生产的付费层。
- 在删除资源前,可通过 Azure 门户使用。
- 在多个 Azure AI 服务中对应用程序使用相同的密钥和终结点。
创建并初始化项目
在 Visual Studio 中创建一个新的 C# 控制台解决方案。 然后将以下内容添加到主代码文件。
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Azure.CognitiveServices.Search.VideoSearch; using Microsoft.Azure.CognitiveServices.Search.VideoSearch.Models;
通过使用你的订阅密钥创建新的
ApiKeyServiceClientCredentials
对象并调用构造函数来实例化客户端。var client = new VideoSearchAPI(new ApiKeyServiceClientCredentials("YOUR-ACCESS-KEY"));
发送搜索请求并处理结果
使用客户端发送搜索请求。 在搜索查询中使用 "SwiftKey"。
var videoResults = client.Videos.SearchAsync(query: "SwiftKey").Result;
如果返回了任何结果,则通过
videoResults.Value[0]
获取第一个结果。 然后输出该视频的 ID、标题和 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!"); }
后续步骤
另请参阅
通过本快速入门开始使用适用于 Java 的必应视频搜索客户端库来搜索新闻。 虽然必应视频搜索具有与大多数编程语言兼容的 REST API,但该客户端库提供了一种简单方法来将服务集成到应用程序中。 此示例的源代码可以在 GitHub 上找到,其中包含其他注释和功能。
先决条件
创建 Azure 资源
通过创建以下 Azure 资源之一开始使用必应视频搜索 API:
- 在删除资源前,可通过 Azure 门户使用。
- 使用免费定价层试用该服务,稍后升级到用于生产的付费层。
- 在删除资源前,可通过 Azure 门户使用。
- 在多个 Azure AI 服务中对应用程序使用相同的密钥和终结点。
通过使用 Maven、Gradle 或其他依赖项管理系统安装必应视频搜索客户端库依赖项。 Maven POM 文件需要以下声明:
<dependencies>
<dependency>
<groupId>com.microsoft.azure.cognitiveservices</groupId>
<artifactId>azure-cognitiveservices-videosearch</artifactId>
<version>0.0.1-beta-SNAPSHOT</version>
</dependency>
</dependencies>
创建并初始化项目
在你最喜欢的 IDE 或编辑器中新建一个 Java 项目,并导入以下库。
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;
创建搜索客户端
实现
VideoSearchAPIImpl
客户端,这需要使用你的 API 终结点以及ServiceClientCredentials
类的实例。public static VideoSearchAPIImpl getClient(final String subscriptionKey) { return new VideoSearchAPIImpl("https://api.cognitive.microsoft.com/bing/v7.0/", new ServiceClientCredentials() { //... } )};
若要实现
ServiceClientCredentials
,请执行以下步骤:重写
applyCredentialsFilter()
函数,使用OkHttpClient.Builder
对象作为参数。//... new ServiceClientCredentials() { @Override public void applyCredentialsFilter(OkHttpClient.Builder builder) { //... } //...
在
applyCredentialsFilter()
中,调用builder.addNetworkInterceptor()
。 创建一个新的Interceptor
对象,并重写其intercept()
方法来获取Chain
侦听器对象。//... builder.addNetworkInterceptor( new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { //... } }); ///...
在
intercept
函数中,为请求创建变量。 使用Request.Builder()
生成请求。 将订阅密钥添加到Ocp-Apim-Subscription-Key
标头,并返回请求对象中的chain.proceed()
。//... 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); } //...
发送搜索请求并接收响应
创建名为
VideoSearch()
的函数,该函数采用字符串形式的订阅密钥。 实例化前面创建的搜索客户端。public static void VideoSearch(String subscriptionKey){ VideoSearchAPIImpl client = VideoSDK.getClient(subscriptionKey); //... }
在
VideoSearch()
中,使用SwiftKey
作为搜索词,通过客户端发送视频搜索请求。 如果视频搜索 API 返回了结果,请获取第一个结果并列显其 ID、名称和 URL,以及返回的视频总数。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!"); } }
从 main 方法调用 search 方法。
public static void main(String[] args) { VideoSDK.VideoSearch("YOUR-SUBSCRIPTION-KEY"); }
后续步骤
另请参阅
通过本快速入门开始使用适用于 JavaScript 的必应视频搜索客户端库来搜索新闻。 虽然必应视频搜索具有与大多数编程语言兼容的 REST API,但该客户端库提供了一种简单方法来将服务集成到应用程序中。 可以在 GitHub 上找到此示例的源代码。 它包含更多注释和功能。
先决条件
- 最新版本的 Node.js。
-
适用于 JavaScript 的必应视频搜索 SDK
- 若要安装,请运行
npm install @azure/cognitiveservices-videosearch
- 若要安装,请运行
-
@azure/ms-rest-azure-js
包中的CognitiveServicesCredentials
类,用于对客户端进行身份验证。- 若要安装,请运行
npm install @azure/ms-rest-azure-js
- 若要安装,请运行
创建 Azure 资源
通过创建以下 Azure 资源之一开始使用必应视频搜索 API:
- 在删除资源前,可通过 Azure 门户使用。
- 使用免费定价层试用该服务,稍后升级到用于生产的付费层。
- 在删除资源前,可通过 Azure 门户使用。
- 在多个 Azure AI 服务中对应用程序使用相同的密钥和终结点。
创建并初始化应用程序
在你喜欢使用的 IDE 或编辑器中创建新的 JavaScript 文件,并添加针对必应视频搜索客户端库和
CognitiveServicesCredentials
模块的require()
语句。 为你的订阅密钥创建变量。const CognitiveServicesCredentials = require('@azure/ms-rest-azure-js').CognitiveServicesCredentials; const VideoSearchAPIClient = require('@azure/cognitiveservices-videosearch');
使用你的密钥创建
CognitiveServicesCredentials
的实例。 然后使用它来创建视频搜索客户端的实例。let credentials = new CognitiveServicesCredentials('YOUR-ACCESS-KEY'); let client = new VideoSearchAPIClient(credentials);
发送搜索请求
使用
client.videosOperations.search()
向必应视频搜索 API 发送搜索请求。 当返回了搜索结果时,使用.then()
来记录结果。client.videosOperations.search('Interstellar Trailer').then((result) => { console.log(result.value); }).catch((err) => { throw err; });
后续步骤
另请参阅
通过本快速入门开始使用适用于 Python 的必应视频搜索客户端库来搜索新闻。 虽然必应视频搜索具有与大多数编程语言兼容的 REST API,但该客户端库提供了一种简单方法来将服务集成到应用程序中。 此示例的源代码可以在 GitHub 上找到,其中包含其他注释和功能。
创建 Azure 资源
通过创建以下 Azure 资源之一开始使用必应视频搜索 API:
- 在删除资源前,可通过 Azure 门户使用。
- 使用免费定价层试用该服务,稍后升级到用于生产的付费层。
- 在删除资源前,可通过 Azure 门户使用。
- 在多个 Azure AI 服务中对应用程序使用相同的密钥和终结点。
先决条件
- Python 2.x 或 3.x
- 适用于 python 的必应视频搜索客户端库
建议使用 Python 虚拟环境。 可以使用 venv 模块安装并初始化虚拟环境。 通过以下命令安装适用于 Python 2.7 的 virtualenv:
python -m venv mytestenv
通过以下操作安装必应视频搜索客户端库:
cd mytestenv
python -m pip install azure-cognitiveservices-search-videosearch
创建并初始化应用程序
在你喜欢使用的 IDE 或编辑器中创建新的 Python 文件,然后添加以下 import 语句。
from azure.cognitiveservices.search.videosearch import VideoSearchClient from azure.cognitiveservices.search.videosearch.models import VideoPricing, VideoLength, VideoResolution, VideoInsightModule from msrest.authentication import CognitiveServicesCredentials
为你的订阅密钥创建变量。
subscription_key = "YOUR-SUBSCRIPTION-KEY" endpoint = "YOUR-ENDPOINT"
创建搜索客户端
创建 CognitiveServicesCredentials
的一个实例并实例化客户端:
client = VideoSearchAPI(endpoint, CognitiveServicesCredentials(subscription_key))
发送搜索请求并获取响应
将
client.videos.search()
与搜索查询配合使用,向必应视频搜索 API 发送请求并获取响应。video_result = client.videos.search(query="SwiftKey")
如果响应包含搜索结果,请获取第一个结果,输出其 ID、名称和 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..")