microsoft / bing-search-sdk-for-net

Bing Search APIs SDK for dot net
MIT License
10 stars 13 forks source link

VisualSearch fails with Unauthorized despite valid key #7

Open palenshus opened 2 years ago

palenshus commented 2 years ago

I feel I must be missing something obvious here. I've been trying to follow the instructions at https://docs.microsoft.com/en-us/bing/search-apis/bing-visual-search/quickstarts/sdk/visual-search-client-library-csharp. I created a "Bing Search" service in Azure with the S9 tier which supports Visual Search. I went to the Keys and Endpoint section and copied the Key 1 out and put it in the below code, yet every time I run it I get Unauthorized:

{"code":"401","message": "Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."}
var client = new VisualSearchClient(new Microsoft.Azure.CognitiveServices.Search.VisualSearch.ApiKeyServiceClientCredentials("<key>"));

ImageInfo ImageInfo = new ImageInfo(url: "https://media.vanityfair.com/photos/5d9f5be40fa2040008f28470/4:3/w_1776,h_1332,c_limit/always-sunny-in-philadelphia-hangs-in-there.jpg");
VisualSearchRequest VisualSearchRequest = new VisualSearchRequest(imageInfo: ImageInfo);

var result = await client.Images.VisualSearchMethodAsync(knowledgeRequest: JsonConvert.SerializeObject(VisualSearchRequest));

The instructions don't say anything about setting an endpoint, but I tried that too, setting the Endpoint property from the one in my Keys and Endpoint page:

client.Endpoint = "https://api.bing.microsoft.com/";

But that just result in a NotFound error.

I was able to get it working by calling the REST api directly, so I know the key isn't the issue. For now I'm calling the REST API but it's a pretty kludgy API to call so I'd much prefer to use the SDK, especially since I can't get the REST code working when uploading an image instead of providing a URL: https://github.com/microsoft/bing-search-dotnet-samples/issues/4

Anyone have any idea what's going on? I tried both keys with no success. Here's a LINQPad repro of the issue: http://share.linqpad.net/c3p8vo.linq

palenshus commented 2 years ago

I figured out the issue. The nuget SDK pointed to by the documentation is incorrect! It says to use the Microsoft.Azure.CognitiveServices.Search.VisualSearch package which is out-of-date, and has the old endpoint in it: https://api.cognitive.microsoft.com. I found that there's another SDK in nuget, Microsoft.Bing.Search.VisualSearch which has the correct endpoint: https://api.bing.microsoft.com, but I can't find documentation anywhere pointing to that! That's pretty dang bad.

rcastoro commented 2 years ago

I have same trouble, NotAuthorized, when I point client to specific endpoint I get NotFound error. SDK broken?