2016-07-29 5 views
0

これまでAzureデータマーケットの「Bing Search」APIを使用して、Objective Cプロジェクトで画像検索を実行していました。Bing Search Ocp-Apim-Subscription-KeyをNSURLSessionに渡すには?

次が検索を実行するコードの一部です:

{ 
     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]; 
    } 

今、私は、現在入手可能なAzureのデータ・マーケット「ビング検索」APIの人生の終わりを発表し、マイクロソフトからの通知を受けました現在、Azureデータマーケット経由でAPIを使用しているユーザーは、その日より前にMicrosoft Cognitive Services Search APIに移行することができます。

この新しいAPIの主な変更点の1つに、Ocp-Apim-Subscription-Key HTTPヘッダーが含まれている必要があります。このヘッダーは、呼び出すAPIのサブスクリプションキーに設定します。

ここでキーを生成しました。 この「Ocp-Apim-Subscription-Key」を渡すために既存のコードを変更するにはどうすればよいですか?

解決策を投稿する場合は、新しい鍵がqwerty12345であるとします。

答えて

1

Ocp-Apim-Subscription-Keyはヘッダーに渡す必要があります。 したがって、NSURLSessionConfigurationとそのメソッドsetHTTPAdditionalHeadersが使用されます:

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