Google Place Request_Denied Horror

时间:2013-07-22 21:25:11

标签: ios objective-c google-places-api

我想使用Google Places API,但我一直在收到Request_Denied。我进入谷歌API控制台,打开谷歌地方API。我的代码是这样的:

    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);

我加倍检查我正在使用正确的API密钥并且我收到此输出:

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

我再次进入Google API控制台并通过Google地方信息开/关开关旁边的“问号”信息按钮点击“试用”,我直接转到另一个具有相同输出的标签。我怎么能解决这个问题?

编辑:添加到网址的多种类型会导致错误

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

1 个答案:

答案 0 :(得分:1)

你有一个!在urlString的末尾。我删除了它,测试了代码并返回结果

    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");
    }