1

On 01.08.12 Bing modified their search api to a Azure, How can I authenticate in Objective-C to use the new bing search api from Azure?

My best guess is to learn from the provided PHP example in the migration word document!! http://go.microsoft.com/fwlink/?LinkID=248077 (Oh god, can't you setup a web page!) or this Java Question - Bing Search API Azure Marketplace Authentication in Java

Community
  • 1
  • 1
Yogev Shelly
  • 1,373
  • 2
  • 13
  • 24
  • here is the solution for the standard HTTPRequest (not ASIHTTPRequest) ` String bing_access=[BING_APP_KEY_AZURE add:[@":""add:BING_APP_KEY_AZURE]]; bing_access=[self encodeBase64:bing_access]; [request setValue:[@"Basic " add:bing_access] forHTTPHeaderField:@"Authorization"]; ` – Anno2001 Aug 17 '12 at 09:30

2 Answers2

3

I'm using ASIHTTPRequest to authenticate with following code.

NSString *queryString = [NSString stringWithFormat:@"'%@'", queryString];
queryString = [queryString urlEncodeUsingEncoding:NSUTF8StringEncoding]; //You'll have to implement url encoding method, preferably in a string category file

NSString *urlString = [NSString stringWithFormat:@"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query=%@&Market='en-US'&$top=50&$format=json", queryString];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlString] ];
[request setAuthenticationScheme:(NSString *)kCFHTTPAuthenticationSchemeBasic];
[request setUsername:@"YOUR_KEY_HERE"];
[request setPassword:@"YOUR_KEY_HERE"];
[request setDelegate:self];
[request startAsynchronous];

Please note, no appID required. just instead pass your key as username and password. It is successfully getting the data.

However, can't really convert the data to NSString. tried every encoding but can't get the string from the data. Initial googling says it's UTF-8 encoded. But no success.

For above code to work, you must add ASIHTTP framework.

Another thing is, my guess is passing base64 encoded string with this format your_key:yourkey should also work with basic authentication.

Mesbah
  • 171
  • 7
  • Thanks, i wasn't aware of - setAuthenticationScheme in ASIHTTPRequest. it works perfect for me as is (removed the urlString part), you might have problem with unescaped strings, for that case you can use the following category (for example i use it to handle HEBREW) , see next comment: – Yogev Shelly Aug 04 '12 at 16:33
  • @implementation NSString (Utils) -(NSString*)encodeForURL { NSString *encodedString = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (__bridge CFStringRef)self, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)); return encodedString ? encodedString : @""; } @end – Yogev Shelly Aug 04 '12 at 16:34
  • From AZURE documentation: You don't need to provide a username. – Yogev Shelly Aug 04 '12 at 16:36
0

I was able to get it to work using just NSUrlConnection. You must first base64encode

NSString *keyString = [NSString stringWithFormat:@"%@:%@", BING_SEARCH_API_KEY, BING_SEARCH_API_KEY];
NSData *plainTextData = [keyString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainTextData base64EncodedString];

Setup your request

NSMutableURLRequest *req = [[NSMutableURLRequest alloc] init];
[req setURL:[NSURL URLWithString:searchUrl]];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", base64String];
[req setValue:authValue forHTTPHeaderField:@"Authorization"];

Make the request

NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];

Look at the documentation about how to form searchUrl, and then process data according to the format you specified in $format= (I used json, so mine looks like):

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

I left out error handling, dont forget to add that by checking response as well as error.