stml
stml

Reputation: 133

Correct Key for Microsoft Cognitive API

I am currently trying to do a News Search on the MS Cognitive Services Bing Search API. I have read many docs, but seem to be stuck.

Here is the code I'm using:

$url = 'https://bingapis.azure-api.net/api/v5/news/search?q=microsoft&mkt=en-us';
$key = '{MY KEY}';
$request_headers = array();
$request_headers[] = 'Ocp-Apim-Subscription-Key: '. $key;
$request_headers[] = 'User-Agent: mozilla';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$data = curl_exec($ch);
curl_close($ch);
echo $data;

This code returns the following result:

{ "statusCode": 401, "message": "Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription." }

As I get the same result from the API Explorer on the site, I think the problem is with my key, rather than my code. But this is the key supplied by Cognitive Services for my subscription, as found on https://www.microsoft.com/cognitive-services/en-us/subscriptions (when logged in).

If this is not the correct key, what should I be using?

Upvotes: 4

Views: 6812

Answers (1)

Ryan Becker
Ryan Becker

Reputation: 56

I'm on the Cognitive Service team at Microsoft. You may hit this issue for the Bing APIs if you generated your subscription keys after 22 June (or renewed your key) and are currently using the https://bingapis.azure-api.net/api/v5/ base URI.

When updating to use the new subscription keys, you must update your application to use the https://api.cognitive.microsoft.com/bing/v5.0/ base URI.

Additionally, If you were using the API Reference for Bing APIs, you can try the following links below which should work with the new keys.

Apologies for the inconvenience and thanks for reporting the issue- we are getting the link updated.

Upvotes: 4

Related Questions