0
votes

So far I was using the Azure Data Market "Bing Search" API to execute Image Searches in my Objective C project.

The following is the portion of code that executes the search:

{
        NSData *authData;
        NSString *authKey = @"<enter Subscription key here!>";
        authData = [[[NSString alloc] initWithFormat:@"%@:%@", authKey, authKey] dataUsingEncoding:NSUTF8StringEncoding];
        NSString *authValue = [[NSString alloc] initWithFormat:@"Basic %@", [self stringByEncodingInBase64:authData]];
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        [config setHTTPAdditionalHeaders:@{@"Authorization": authValue}];

        // Timeout settings...
        config.timeoutIntervalForRequest = 6.0;
        config.timeoutIntervalForResource = 8.0;

        NSMutableCharacterSet * URLQueryPartAllowedCharacterSet;
        URLQueryPartAllowedCharacterSet = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy];
        [URLQueryPartAllowedCharacterSet removeCharactersInString:@"&+=?"];
        NSString * escapedValue = [searchKeys stringByAddingPercentEncodingWithAllowedCharacters:URLQueryPartAllowedCharacterSet];
        NSString * urlString = [[NSString alloc] initWithFormat:
            @"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='%@'&$top=20&$format=json", escapedValue];
        NSURL *JSONURL = [NSURL URLWithString:urlString];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
        NSURLSessionDataTask * dataTask =
        [[NSURLSession sessionWithConfiguration:config] dataTaskWithRequest:request completionHandler:^
        (NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {                                                                
         < PROCESS YOUR DATA HERE >    
         }];
        [dataTask resume];
     }

Now, I have received a notification from Microsoft, announcing the end of life of the currently available Azure Data Market "Bing Search" API offerings on December 15, 2016. Users who are currently using the APIs via the Azure Data Market will have the option to migrate to the Microsoft Cognitive Services Search API offerings prior to that date.

One of the main changes with this new API is that each request that you make must include the Ocp-Apim-Subscription-Key HTTP header, which you set to the subscription key of the API that you're calling.

I have generated the key now. How can I modify my existing code to pass this "Ocp-Apim-Subscription-Key" ?

Suppose that the new key is qwerty12345, if you are going to post the solution.

1

1 Answers

1
votes

Ocp-Apim-Subscription-Key should be passed in the Header. Hence, NSURLSessionConfiguration and its method setHTTPAdditionalHeaders will be used:

NSString *authKey = @"<enter NEW key>";
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
[config setHTTPAdditionalHeaders:@{@"Ocp-Apim-Subscription-Key": authKey}];