編輯

共用方式為


Handling the news search response

When you send a request to News Search API, it returns a NewsAnswer object in the response body. The object may include one or more of the following fields:

{
  "_type": "News",
  "readLink": "https://api.bing.microsoft.com/api/v7/news/search?q=us+politics",
  "queryContext": { ... },
  "totalEstimatedMatches": 9890000,
  "sort": [ { ... } ],
  "value": [ { ... } ]
}

But if an error occurs, the response body contains an ErrorResponse object. Bing returns an error response for all 400 level HTTP status codes. Read more

{
  "_type": "ErrorResponse", 
  "errors": [
    {
      "code": "InvalidAuthorization", 
      "subCode": "AuthorizationMissing", 
      "message": "Authorization is required.", 
      "moreDetails": "Subscription key is not recognized."
    }
  ]
}

Note

Because URL formats and parameters are subject to change without notice, use all URLs in Bing search results as-is. You should not take dependencies on the URL format or parameters except where noted.

The NewsAnswer object's value field contains a list of NewsArticle objects. Here's what most of the news objects look like in the response.

    {
      "name": "Hunger to break the old political system grows",
      "url": "https:\/\/www.contosonews.com\/politics\/politics-news...",
      "image": {
        "thumbnail": {
          "contentUrl": "https:\/\/www.bing.com\/th?id=ON.0430E0FD42944...",
          "width": 700,
          "height": 367
        }
      },
      "description": "As they kick off an all-virtual convention this week, Democrats aren't...",
      "provider": [
        {
          "_type": "Organization",
          "name": "Contoso News",
          "image": {
            "thumbnail": {
              "contentUrl": "https:\/\/www.bing.com\/th?id=AR_e4bde9ad3949725..."
            }
          }
        }
      ],
      "datePublished": "2020-08-15T15:55:00.0000000Z"
    },

Each news article includes the article's name, description, image, and url to the article on the host's website. Be sure to use provider to attribute the article.

If Bing can determine the category of news article, the article includes the category field.

      "category": "Politics"

Some articles include the video field, which contains a video that's relevant to the news story. Bing provides an iFrame that you can embed in your HTML to play the video.

      "video": {
        "name": "The Political Conventions Are Starting...",
        "thumbnailUrl": "https:\/\/www.bing.com\/th?id=ON.C34094BFB2230182BC...",
        "embedHtml": "<iframe title=\"Contoso News Video - Embed Player\" width=\"480\" height=\"321\" frameborder=\"0\" scrolling=\"no\" allowfullscreen=\"true\" marginheight=\"0\" marginwidth=\"0\" id=\"contoso_video_player\" src=\"https:\/\/www.contosonews.com\/video\/players\/offsite\/...\"><\/iframe>",
        "allowHttpsEmbed": true,
        "thumbnail": {
          "width": 75,
          "height": 75
        }
      },

Note that in some cases, the Video object includes only a thumbnail and not the embedded video.

      "video": {
        "name": "US politics: virtual Democratic convention to start amid coronavirus crisis – live updates",
        "thumbnailUrl": "https:\/\/www.bing.com\/th?id=ON.0C2BD59684791402EB8ED247AC9C4B58&pid=News",
        "thumbnail": {
          "width": 480,
          "height": 288
        }
      },

Next steps