question

MikeTaranov-9159 avatar image
0 Votes"
MikeTaranov-9159 asked romungi-MSFT commented

Bing News API Key not working

I am trying to use Bing News api v7.
I got the key from Bing resources > name of resource > Keys and Endpoint.

When I try to get information from the endpoint, I am getting this.

HTTPError: 401 Client Error: Access Denied for url: https://api.bing.microsoft.com/v7.0/news/search?q=Microsoft&mkt=en-US

azure-bing-news
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Code:

import json
import os
from pprint import pprint
import requests


Add your Bing Search V7 subscription key and endpoint to your environment variables.
subscriptionKey = ''
endpoint = 'https://api.bing.microsoft.com/v7.0/news/search'

query = "Microsoft"

Construct a request
mkt = 'en-US'
params = {'q': query, 'mkt': mkt}
headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}

Call the API
try:
response = requests.get(endpoint, headers=headers, params=params)
response.raise_for_status()

print("\nHeaders:\n")
print(response.headers)
print("\nJSON Response:\n")
pprint(response.json())
except Exception as ex:
raise ex


Error:

HTTPError: 401 Client Error: Access Denied for url: https://api.bing.microsoft.com/v7.0/news/search?q=Microsoft&mkt=en-US

0 Votes 0 ·

anonymous user I tried the above snippet and it returned a valid response with results with the key of my resource. I think your resource might be older i.e created before October 2020 i,e before bing search got migrated from cognitive services. Could you please try the same with a new resource and check if it works?

85270-image.png


0 Votes 0 ·
image.png (94.5 KiB)

@romungi-MSFT, thank you for feedback. My bing resource has been created on April 6th, 2021.

0 Votes 0 ·
Show more comments

1 Answer

MikeTaranov-9159 avatar image
0 Votes"
MikeTaranov-9159 answered romungi-MSFT commented

@romungi-MSFT , thank you. I had to re-create my Bing Search resource to resolve this issue.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

anonymous user That's great. I have moved your comment to answer, please feel free to accept.

0 Votes 0 ·