HusseinB
HusseinB

Reputation: 1341

Google Place Request_Denied Horror

i want to use Google Places API but i keep getting a Request_Denied. i entered to the Google API Console, turned on google places API. my code goes like this:

    NSString *searchString = [NSString stringWithFormat:@"Beirut"];
    NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/textsearch/json?query=%@&sensor=true&key=%@!",searchString, kGOOGLE_API_KEY];

    NSURL *requestURL = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:(requestURL)];

    //response
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSError *jsonParsingError = nil;
    NSDictionary *locationResults = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

    NSLog(@"%@",locationResults);

i doubled checked that i'm using the correct API key and i receive this output:

{
    "debug_info" =     (
    );
    "html_attributions" =     (
    );
    results =     (
    );
    status = "REQUEST_DENIED";
}

i entered again to the Google API console and clicked on "Try" through the "question mark" info button next to the Google Places on/off switch and i was direct to another tab having the same output. how can i solve this?

Edit: multi-type added to URL causes an error

NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&types=food|bar&sensor=true&key=%@", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude, [NSString stringWithFormat:@"%i", 1000], kGOOGLE_API_KEY];

Upvotes: 1

Views: 322

Answers (1)

Yan
Yan

Reputation: 3616

You have an ! at the end of the urlString. I deleted it, tested the code and it returns the results

    NSString *searchString = [NSString stringWithFormat:@"NewYork"];
    NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/textsearch/json?query=%@&sensor=true&key=%@",searchString, kGOOGLE_API_KEY];

    NSURL *requestURL = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:requestURL];

    //response
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if(response){
        NSError *jsonParsingError = nil;
        NSDictionary *locationResults = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

        NSLog(@"%@",locationResults);
    }else{
        NSLog(@"nil");
    }

Upvotes: 1

Related Questions