user2502870
user2502870

Reputation: 11

Problem submitting Bing API search request from apps script

I'm implementing the bing api search in google apps script.

I've tried this approach:

var url =https://api.bing.microsoft.com/v7.0/search?key=[appkey]&q=Bart's%20Heating%20%26%20Air var response = UrlFetchApp.fetch(url);

And this approach:

var url = https://api.bing.microsoft.com/v7.0/search?q=Bart's%20Heating%20%26%20Air var options = { 'method': 'GET', 'headers': { 'Ocp-Apim-Subscription-Key': apiKey } }; var response = UrlFetchApp.fetch(url, options);

In both cases, the error I'm getting is: "Exception: Request failed for https://api.bing.microsoft.com returned code 401. Truncated server response: {"error":{"code":"401","message":"Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an acti..."

My subscription is active, the API key was copied from Azure settings and I believe endpoint is correct.

Any ideas what the problem might be?

Upvotes: 0

Views: 73

Answers (2)

user2502870
user2502870

Reputation: 11

The problem ended up being terminology. All 3 of the code samples refer to a subscription key not an api key. Since there is no api key on the main Azure settings page for a web search, I used the subscription id. After I found and used one of the api keys on the Manage keys page things are now working fine.

Upvotes: 1

Sampath
Sampath

Reputation: 3488

The error 401 - Access denied due to invalid subscription key or wrong API endpoint occurs because the URL contains the instance's configuration ID.

var endpoint = "https://api.bing.microsoft.com/";
 var url = endpoint + "/v7.0/custom/images/search?" + "q=" + searchTerm + "&customconfig=" + customConfigId

Create a Bing Custom Search in Azure, copy the BING_CUSTOM_SEARCH_SUBSCRIPTION_KEY. You can obtain the customConfigId from this link.

The API call will be as follows:

export BING_CUSTOM_SEARCH_SUBSCRIPTION_KEY="your_subscription_key_here"
 export YOUR_CUSTOM_CONFIG_ID="your_custom_config_id_here"

curl --header "Ocp-Apim-Subscription-Key: $BING_CUSTOM_SEARCH_SUBSCRIPTION_KEY""https://api.bing.microsoft.com/v7.0/custom/search?q=car&customconfig=$YOUR_CUSTOM_CONFIG_ID&mkt=en-US"

Below is the code for making a custom search request using the Bing API with Axios.

const axios = require('axios');

const subscriptionKey = ' Your_Bing_API_subscription_key'; 
const customConfig = 'Your_custom_config_ID'; 
const query = 'car'; 
const market = 'en-US'; 

const url = `https://api.bing.microsoft.com/v7.0/custom/search?q=${encodeURIComponent(query)}&customconfig=${customConfig}&mkt=${market}`;

axios.get(url, {
    headers: {
        'Ocp-Apim-Subscription-Key': subscriptionKey
    }
})
.then(response => {
    console.log('Search Results:', response.data);
})
.catch(error => {
    if (error.response) {
        
        console.error('Error Response:', error.response.data);
        console.error('Status Code:', error.response.status);
    } else if (error.request) {
       
        console.error('Error Request:', error.request);
    } else {
       
        console.error('Error Message:', error.message);
    }
});

Output: enter image description here

Refer to this document for Bing Custom Search in C#. Additionally, check this git for information on how to add Bing web search capabilities to your application.

Upvotes: 0

Related Questions