java Server returned HTTP response code: 401 for URL: https://api.bing.microsoft.com/v7.0/search?q= having a valid API KEY ?

Raffaele Silvestri 0 Reputation points
2023-09-20T16:46:54.1066667+00:00
//I like to learn Java. Thanks for your reply.

public class BingAPI {

	// Add your Bing Search V7 subscription key to your environment variables.
	static String host = "https://api.bing.microsoft.com";
	static String path = "/v7.0/search";
	static String searchTerm = "Microsoft Bing Search Services";
	static String subscriptionKey = "3a02...";
    //static String subscriptionKey = System.getenv("BING_ACCOUNT_KEY");
	
	public BingAPI() {
    }
    
	public String getAnswerFromBING (String searchQuery) throws Exception {

        //----------------------------Bing search-----------------------
		searchQuery = searchQuery.replaceAll(" ", "%20");
        String result = "";
        
        	// Construct the URL.
            URL url = new URL(host + path + "?q=" +  URLEncoder.encode(searchQuery, "UTF-8")+ "&mkt=it_IT&count=50&format=json");
 
            // Open the connection.
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
	        conn.setRequestMethod("GET");
	        conn.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
	
	        // Receive the JSON response body.        
	        BufferedReader br = new BufferedReader(new InputStreamReader(
	                (conn.getInputStream())));
	        StringBuilder sb = new StringBuilder();
	        String output;
	        System.out.println("Output from Server .... \n");
	        char[] buffer = new char[4096];
	        while ((output = br.readLine()) != null) {
	            sb.append(output);
	
	            //  text.append(link + "\n\n\n");//Will print the google search links   
	        } 
	
	        conn.disconnect(); 
Bing Custom Search
Bing Custom Search
An easy-to-use, ad-free, commercial-grade search tool that lets you deliver the results you want.
82 questions
{count} votes

2 answers

Sort by: Most helpful
  1. YutongTie-MSFT 47,336 Reputation points
    2023-09-20T19:59:07.8066667+00:00

    Hello @Raffaele Silvestri

    Thanks for reaching out to us, you need to add your Azure Subscription ID to your environment and uncomment your code below -

    static String subscriptionKey = System.getenv("BING_ACCOUNT_KEY");

    Or, you can input the subscription Key in your code directly as below -

    // Enter a valid subscription key.

    static String subscriptionKey = "enter key here";

    Please let us know if you have any further questions. Thanks.

    Document you may want to refer to - https://learn.microsoft.com/en-us/bing/search-apis/bing-web-search/quickstarts/rest/java

    Regards,

    Yutong

    0 comments No comments

  2. Raffaele Silvestri 0 Reputation points
    2023-09-22T10:54:35.23+00:00

    I did an Azure free subscription, providing required CC information, so I have now Subscription ID (tenant ID, Object ID). I used either Subscription ID and API Dialogflow accountkey.

    Request is refused, this is the error i get:

    {"error":{"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."}}

    I'm a bit confused, which key should I use in the

    conn.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);

    to get from google internet information.

    Do I have to require a new API Key after the Azure subscription or the one I requested before the subscription is valid anyway ?.

    Thanks a lot to clarify