2016-07-29 2 views
0

지금까지 Azure 데이터 마켓 "Bing Search"API를 사용하여 Objective C 프로젝트에서 이미지 검색을 실행했습니다.Bing 검색 방법 NSURLSession에서 Ocp-Apim-Subscription-Key를 전달 하시겠습니까?

다음은 검색 실행되는 코드의 일부입니다 : 이제

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

, 나는 현재 사용 가능한 푸른 데이터 시장의 수명을 발표, Microsoft에서 알림을받은 "빙 검색"API 2015 년 12 월 15 일에 제공됩니다. 현재 Azure 데이터 마켓을 통해 API를 사용하는 사용자는 해당 날짜 전에 Microsoft인지 서비스 검색 API 제품으로 마이그레이션 할 수 있습니다.

이 새로운 API의 주요 변경 사항 중 하나는 호출하는 API의 구독 키로 설정 한 Ocp-Apim-Subscription-Key HTTP 헤더를 포함해야한다는 것입니다.

지금 키를 생성했습니다. "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}]; 
관련 문제