Migration keep fail from "api.search.live.net/json.aspx"

Answered Migration keep fail from "api.search.live.net/json.aspx"

  • Thursday, August 02, 2012 4:17 PM
     
     

    Hello guru,

    I have a silly question, which to me is "hard", really need you to help out.

    Originally I use this "api.search.live.net/json.aspx" to parse and fetch image data, however, after August 1st, everything down because the system turned into Azure. I obtained a new App Key with paid service, however, I still don't have a clue how to change from the old one into a new one.

    My old code:

    NSString * const BING_SEARCH_PATH = @"http://api.search.live.net/json.aspx";

    NSString * const APP_ID = @"5B0D22D739247C06BE7F990ECBEC1A144F9B7C39";

    I tried to change into this but still doesn't work

    NSString * const BING_SEARCH_PATH = @"http://api.bing.net/json.aspx?AppId= YOUR_APPID &Version=2.2&Market=en-US&Query=testign&Sources=web+spell&Web.Count= 1";

    Any advice?

    Thanks a million.


All Replies

  • Thursday, August 02, 2012 6:23 PM
     
     
    I suggest you take a look at the new docs, especially the migration guide. The root service URIs are different and there's a few other things you'll need to change. You can get the docs on the Bing Search API Azure Marketplace page.  Hopefully, that will help you get started.
  • Thursday, August 02, 2012 7:21 PM
     
     

    Thanks Victor,

    I go through the Migration again, tried all the ways they suggest, but still return "Request fail" or "NO Result".

    I changed from

    NSString * const BING_SEARCH_PATH = @"http://api.search.live.net/json.aspx";

    into 

    NSString * const BING_SEARCH_PATH = @"https://api.datamarket.azure.com/Bing/Search/Image?Query=%27odata%27&$top=2";

    NSString * const APP_ID = @"[myappId]"; // If i wasn't wrong, my App Id should be the same as My primary account key. I know there is another one called "custom ID", do you think i should try that as well?

    Test results: 

    2012-08-02 15:13:19.077 WebImagePicker[17788:f803] WebImageSearch.searchTerm.url: https://api.datamarket.azure.com/Bing/Search/Image?Query=%27odata%27&$top=2?AppId=[myappId]=&Sources=image&Query=APPLE&Image.Count=50&Image.Offset=0&Image.Filters=Size:Large

    2012-08-02 15:13:21.075 YSWebImagePicker[17788:f803] YSWebImageSearch.requestFailed: Error Domain=ASIHTTPRequestErrorDomain Code=3 "Authentication needed" UserInfo=0x6e8eab0 {NSLocalizedDescription=Authentication needed}

    Thanks again:)


  • Thursday, August 02, 2012 7:45 PM
     
     

    It looks like you're having a problem with passing your account key. You can use your default account key (the one created for you when you signed up), or you can use one that you create. Either one works.

    But I don't see in your code how you're passing that account key to Bing. Basically, you need to send an HTTP authorization header with your request to api.datamarket.azure.com/Bing/Search/etc.

    Authorization: Basic xxxx

    where xxxx is the base 64 encoded string of this: ":accountKey"  or "accountKey:accountKey" - bascially, doesn't matter what's on the left of the colon (:) but you need the colon.

    hth,

    Victor David

  • Friday, August 03, 2012 1:20 AM
     
     

    Thanks a lot for your patience and time Victor, but I still failed, if you got a minutes, could you take a quick look at my codes, I just don't know which part has problem.  JSON / OBJECTIVE-C

    #import "YSWebImageSearch.h"

    #import "ASIHTTPRequest.h"
    #import "NSString+Utils.h"

    @interface YSWebImageSearch() <ASIHTTPRequestDelegate>

    -(NSArray*)imagesFromJsonReponse:(NSString*)response;
    -(void)fetchImages;
    -(void)reset;
    @property (readwrite, copy) NSArray *images;
    @property (nonatomic, strong) NSString* searchTerm;
    @end

    @implementation YSWebImageSearch
    @synthesize imageCountPerPage =_imageCountPerPage;
    @synthesize images =_images;
    @synthesize searchTerm = _searchTerm;
    @synthesize delegate;
    NSString * const BING_SEARCH_PATH = @"https://api.datamarket.azure.com/Bing/Search/Image?Query=%27odata%27&$top=2";
    NSString * const APP_ID = @"F4KeyYz+0vCb44/BaJADkkJHDt/A3woy3g5GogYaw/H=";

    int IMAGE_COUNT_PER_PAGE = 5;
    int MAX_IMAGES_PER_PAGE = 50;
    int pageIndex =0;

    //---------------------------------------------------
    #pragma mark - Public API
    //---------------------------------------------------

    -(NSNumber*)imageCountPerPage

    {

        if(!_imageCountPerPage)
            _imageCountPerPage = [NSNumber numberWithInt:IMAGE_COUNT_PER_PAGE];
        return _imageCountPerPage;

    }

    -(void)setImageCountPerPage:(NSNumber *)imageCountPerPage
    {
        _imageCountPerPage = [imageCountPerPage intValue] > MAX_IMAGES_PER_PAGE ? 
                    [NSNumber numberWithInt:MAX_IMAGES_PER_PAGE] : imageCountPerPage;

    }

    -(void)searchImagesForTerm:(NSString *)term
    {
        [self reset];
        self.searchTerm = term;
        [self fetchImages];

    }

    -(void)fetchNextPage{
        [self fetchImages];
    }

    -(void)setSource:(eSearchSource)searchSource
    {
        //not implemented
    }

    //---------------------------------------------------

    #pragma mark - Private API

    //---------------------------------------------------

    -(void)requestFinished:(ASIHTTPRequest *)request
    {

        NSString *responseString = [request responseString];       
        NSArray* newImages =[self imagesFromJsonReponse:responseString];

        if(!newImages || ![newImages count])
        {
            [self reset];
            [delegate YSWebImageSearchFailed:eWebImageSearchFail_NoResults];

        }
        else
        {
            pageIndex++;
            self.images = [self.images arrayByAddingObjectsFromArray:newImages];
            NSLog(@"YSWebImageSearch.requestFinished.images: %@",self.images);
            [delegate YSWebImageSearchImagesDataUpdated:self.images];
        }

    }

    -(void)requestFailed:(ASIHTTPRequest *)request

    {

        NSError *error = [request error];
        NSLog(@"YSWebImageSearch.requestFailed: %@",error.description);
        [self reset];
        [delegate YSWebImageSearchFailed:eWebImageSearchFail_NoInternet];

    }

    -(void)reset
    {
        pageIndex =0;
        self.images = [NSArray array];

    }

    -(void)fetchImages

    {

        NSString* escapedSearchTerm = [self.searchTerm encodeForURL]; //@"%D7%A9%D7%9E%D7%A9";

        NSString* varAppId = [NSString stringWithFormat:@"?AppId=%@",APP_ID];

        NSString* varQuery = [NSString stringWithFormat:@"&Query=%@",escapedSearchTerm];

        NSString* varImageCount =  [NSString stringWithFormat:@"&Image.Count=%@",self.imageCountPerPage];

        NSString* varImageOffset =  [NSString stringWithFormat:@"&Image.Offset=%i",pageIndex * [self.imageCountPerPage intValue]+pageIndex];

        NSString* varSources = @"&Sources=image";

        NSString* varImageFilters  = @"&Image.Filters=Size:Large";

        

        NSString* requestString = [NSString stringFromStringArray:
                                   [NSArray arrayWithObjects:BING_SEARCH_PATH,
                                    varAppId,varSources,varQuery,
                                    varImageCount,varImageOffset,varImageFilters,nil]];
         NSURL *url = [NSURL URLWithString:requestString];
        NSLog(@"YSWebImageSearch.searchTerm.url: %@",url);
        ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:url];
        request.delegate = self;
        [request startAsynchronous];

    }

    -(NSArray*)imagesFromJsonReponse:(NSString*)response 

    {
        //1.format to json
        //2. create YSImageData from json
        NSError* error;
        NSDictionary *JSON = 
        [NSJSONSerialization JSONObjectWithData: [response dataUsingEncoding:NSUTF8StringEncoding] 
                                        options: NSJSONReadingMutableContainers 
                                          error:&error];

        if(error)
        {
            NSLog(@"YSWebImageSearch.imagesFromJsonReponse - Error: %@",error.description);
            return nil;

        }

        NSArray* images = [[[JSON objectForKey:@"SearchResponse"]
                            objectForKey:@"Image"] 
                           objectForKey:@"Results"];

        NSMutableArray* webImageDataArray = [NSMutableArray array];
        for(int i=0;i<[images count];i++)
        {
            YSWebImageData* imageData = [YSWebImageData imageDataFromBingImageData:
                                         [images objectAtIndex:i]];
            [webImageDataArray addObject:imageData];
        }

        return webImageDataArray;

    }

    @end

  • Friday, August 03, 2012 1:59 AM
     
     Answered

    I'm not familiar with the programming language you're using, but it does appear that you're not assembling the service URI correctly. You changed the service root in your BING_SEARCH_PATH, but you're putting in parms (like AppId and Image.Count) that aren't applicable for the new API.

    Beyond that, I'll have to defer to someone who knows this programming language better than me. Meanwhile, study the docs some more to find out which parameters need to be changed. Also, you'll need to figure out how to make an HTTP header for your account key using your programming language.

    You can try the new service URI directly in your browser address bar. You'll get prompted for your key, and the results in your browser will be raw XML (or JSON if you tell Bing that's what you want) but it will help you learn how the new parameters work. I do know that if you pass bad parms (that is, ones Bing doesn't recognize), it won't just ignore them; it will cause the request to fail. So trying things out directly in the browser can be helpful for learning.

    Victor David


  • Friday, August 03, 2012 7:37 AM
     
     

    Victor, Thanks for your kind support. What you suggest helps. 

    Meanwhile, i am looking for more reference, once i figure it out, I'll post it here for sharing:)

    HappyNutella

  • Tuesday, August 07, 2012 10:32 PM
     
     

    Did you solve your issue? I'm having the same problem. 

    Please email me if you have an answer emailfelixt at gmail.com