Обычный вызов API из URL-адреса в API с ключом API работает, но код на C# выдает Not Found по тем же запросам из кода.
Пожалуйста, ребята, не поймите слишком строго, Gemini и GPT говорят, что все в порядке.
Это код одного из моих сервисов в моем приложении микросервисов
Что еще странно, так это то, что я даже не получаю логи с _логгер
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DestinationService.Models;
using Nest;
using Elasticsearch.Net;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging.Console;
namespace DestinationService.Services
{
public class GoogleMapsService
{
private readonly string _apiKey;
private readonly ILogger _logger;
public GoogleMapsService(IConfiguration configuration, ILogger logger)
{
_apiKey = configuration["GoogleMaps:ApiKey"];
_logger = logger;
}
public async Task GetPlaceDetails(string query)
{
var client = new HttpClient();
var searchResponse = await client.GetAsync(
$"https://maps.googleapis.com/maps/api/pl ... y={_apiKey}"
);
searchResponse.EnsureSuccessStatusCode();
var searchContent = await searchResponse.Content.ReadAsStringAsync();
var searchResult = JsonConvert.DeserializeObject(searchContent);
if (searchResult.Candidates == null || !searchResult.Candidates.Any())
{
throw new Exception("No matching places found for the given query.");
}
var placeId = searchResult.Candidates.First().PlaceId;
var detailsResponse = await client.GetAsync(
$"https://maps.googleapis.com/maps/api/pl ... y={_apiKey}"
);
detailsResponse.EnsureSuccessStatusCode();
return await detailsResponse.Content.ReadAsStringAsync();
}
public async Task SearchNearby(string location, int radius, string keyword = "")
{
var client = new HttpClient();
_logger.LogInformation($"{client}");
var url = $"https://maps.googleapis.com/maps/api/pl ... y={_apiKey}";
if (!string.IsNullOrEmpty(keyword))
url += $"&keyword={Uri.EscapeDataString(keyword)}";
_logger.LogInformation($"{url}");
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
}
using System.Threading.Tasks;
using DestinationService.Services;
using Microsoft.AspNetCore.Mvc;
using DestinationService.Models;
using System.Collections.Generic;
using System;
using DestinationService.Data;
namespace DestinationService.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class DestinationsController : ControllerBase
{
private readonly DestinationSearchService _searchService;
public DestinationsController(DestinationSearchService searchService)
{
_searchService = searchService;
}
[HttpGet]
[HttpGet]
public async Task GetDestinations(
[FromQuery] string query,
[FromQuery] string? location,
[FromQuery] decimal? minCost,
[FromQuery] decimal? maxCost,
[FromQuery] string? activities)
{
var results = await _searchService.SearchDestinations(query, location, minCost, maxCost, activities);
return Ok(results);
}
[HttpPost]
public async Task AddDestination(
[FromBody] Destination destination,
[FromServices] GoogleMapsService googleMapsService,
[FromServices] AppDbContext dbContext)
{
if (!string.IsNullOrEmpty(destination.GooglePlaceId))
{
var placeDetails = await googleMapsService.GetPlaceDetails(destination.GooglePlaceId);
destination.PlaceDetails = placeDetails;
}
dbContext.Destinations.Add(destination);
await dbContext.SaveChangesAsync();
await _searchService.IndexDestination(destination);
return CreatedAtAction(nameof(AddDestination), destination);
}
[HttpGet("search-nearby")]
public async Task SearchNearby(
[FromQuery] string location,
[FromQuery] int radius,
[FromQuery] string? keyword,
[FromServices] GoogleMapsService googleMapsService)
{
var results = await googleMapsService.SearchNearby(location, radius, keyword);
return Ok(results);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... c-sharp-gi
Обычный вызов API из URL-адреса в API с ключом API работает, но код на C# выдает Not Found по тем же запросам из кода. ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение