I am trying to use the Bing Search API V7 through my Azure subscription, but I am receiving the following error 401 error. The key is enabled.
The code i use is the following:
static async Task<string> GetImageFromBing(string ean)
{
string apiKey = "mCp6RsHZNhetYTLIfIMecsdDp5UgBUWCZG6rkZIv12AzSeCCygz4"; // Assicurati che questa sia corretta.
string endpoint = $".0/images/search?q={Uri.EscapeDataString(ean)}+prodotto";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);
try
{
var response = await client.GetStringAsync(endpoint);
using JsonDocument doc = JsonDocument.Parse(response);
var value = doc.RootElement.GetProperty("value");
if (value.GetArrayLength() > 0)
{
var firstImage = value[0].GetProperty("contentUrl").GetString();
return firstImage ?? "";
}
}
catch (Exception ex)
{
Console.WriteLine($"❌ Errore durante la ricerca dell'immagine: {ex.Message}");
}
}
return "";
}
How to make things working?
I am trying to use the Bing Search API V7 through my Azure subscription, but I am receiving the following error 401 error. The key is enabled.
The code i use is the following:
static async Task<string> GetImageFromBing(string ean)
{
string apiKey = "mCp6RsHZNhetYTLIfIMecsdDp5UgBUWCZG6rkZIv12AzSeCCygz4"; // Assicurati che questa sia corretta.
string endpoint = $"https://api.bing.microsoft/v7.0/images/search?q={Uri.EscapeDataString(ean)}+prodotto";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);
try
{
var response = await client.GetStringAsync(endpoint);
using JsonDocument doc = JsonDocument.Parse(response);
var value = doc.RootElement.GetProperty("value");
if (value.GetArrayLength() > 0)
{
var firstImage = value[0].GetProperty("contentUrl").GetString();
return firstImage ?? "";
}
}
catch (Exception ex)
{
Console.WriteLine($"❌ Errore durante la ricerca dell'immagine: {ex.Message}");
}
}
return "";
}
How to make things working?
Share Improve this question edited Feb 17 at 22:32 James Z 12.3k10 gold badges27 silver badges47 bronze badges asked Feb 16 at 14:19 Simone SpagnaSimone Spagna 6349 silver badges30 bronze badges 5- 2 Well first you should revoke and regenerate your API key. Posting your secret value to the public internet isn't a good idea. – gunr2171 Commented Feb 16 at 15:15
- it is a old api key – Simone Spagna Commented Feb 16 at 20:09
- @SimoneSpagna can you try like this in the git – Sampath Commented Feb 17 at 5:14
- We appreciate your interest in Bing APIs. However, please note that Bing Search APIs with your LLM will be retired on March 6, 2025. Existing instances will be fully decommissioned, and new customer sign-ups will no longer be supported. To continue using Bing search capabilities, we recommend transitioning to Grounding with Bing Search as part of Azure AI Agents. This solution allows your Azure AI Agents to incorporate real-time public web data when generating responses with an LLM. – Sampath Commented yesterday
- @Simone Spagna To avoid service disruptions, follow this guide to migrate to Grounding with Bing Search before March 6, 2025. – Sampath Commented yesterday
1 Answer
Reset to default 0From this doc ,Bing Search APIs with your LLM will be retired on March 6, 2025. Existing instances will be fully decommissioned, and new customer sign-ups will no longer be supported.
To continue using Bing search capabilities, we recommend transitioning to Grounding with Bing Search as part of Azure AI Agents. This solution enables your Azure AI Agents to incorporate real-time public web data when generating responses with an LLM.
To avoid service disruptions, follow this guide to migrate to Grounding with Bing Search before March 6, 2025.
Refer to this Microsoft documentation to create a new Azure AI Agent Service project and obtain the connection string. Additionally, check out this guide for code implementation.
Response<ThreadRun> runResponse = await agentClient.CreateRunAsync(thread, agent);
do
{
await Task.Delay(TimeSpan.FromMilliseconds(500));
runResponse = await agentClient.GetRunAsync(thread.Id, runResponse.Value.Id);
}
while (runResponse.Value.Status == RunStatus.Queued
|| runResponse.Value.Status == RunStatus.InProgress);
Response<PageableList<ThreadMessage>> afterRunMessagesResponse
= await agentClient.GetMessagesAsync(thread.Id);
IReadOnlyList<ThreadMessage> messages = afterRunMessagesResponse.Value.Data;
foreach (ThreadMessage threadMessage in messages)
{
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
foreach (MessageContent contentItem in threadMessage.ContentItems)
{
if (contentItem is MessageTextContent textItem)
{
Console.Write(textItem.Text);
}
else if (contentItem is MessageImageFileContent imageFileItem)
{
Console.Write($"<image from ID: {imageFileItem.FileId}");
}
Console.WriteLine();
}
}