1

我想使用 Google Places API,但我不断收到 Request_Denied。我进入了 Google API 控制台,打开了 Google Places 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 Places 开/关开关旁边的“问号”信息按钮单击“尝试”,然后我直接进入另一个具有相同输出的选项卡。我该如何解决这个问题?

编辑:多类型添加到 URL 会导致错误

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

1 回答 1

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");
    }
于 2013-07-22T23:32:31.003 回答