Blazor 웹 애플리케이션에 Power BI 대시 보드를 포함하는 작업을하고 있지만 액세스 토큰을 검색하는 데 완전히 멈춰있는 것 같습니다. 이 자습서 에서 가져온 다음 코드 를 사용하고 있으며 암호 대신 클라이언트 자격 증명을 사용하도록 수정했습니다.
private async Task<string> GetPowerBIAccessToken()
{
using (var client = new HttpClient())
{
var form = new Dictionary<string, string>();
form["grant_type"] = "client_credentials";
form["client_id"] = _configuration["PowerBI:ApplicationId"];
form["client_secret"] = _configuration["PowerBI:ApplicationSecret"];
form["scope"] = "https://graph.microsoft.com/.default";
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");
var request = new HttpRequestMessage();
request.SetBrowserRequestMode(BrowserRequestMode.NoCors);
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(_configuration["PowerBI:AuthorityUrl"]);
request.Content = new FormUrlEncodedContent(form);
using (var response = await client.SendAsync(request))
{
var body = await response.Content.ReadAsStringAsync();
var jsonBody = JObject.Parse(body);
var errorToken = jsonBody.SelectToken("error");
if (errorToken != null)
{
throw new Exception(errorToken.Value<string>());
}
return jsonBody.SelectToken("access_token").Value<string>();
}
}
}
이 코드는 브라우저에서 다음 오류를 생성합니다 Unhandled exception rendering component: Error reading JObject from JsonReader. Path '', line 0, position 0.
..
라인 var jsonBody = JObject.Parse(body)
이 JSON 응답을 구문 분석하려고하는데 응답이 비어 있기 때문 입니다. 당황스럽게도 이것은 요청이 실패했기 때문이 아니라 실제로 200을 반환하지만 완전히 빈 응답 본문을 사용합니다.
더욱 난해하게도 Postman에서 재구성 된 똑같은 요청이 결과를 제대로 반환합니다.
나는 Postman과 내가 간과하고있는 내 코드 사이에 몇 가지 기본적인 차이점이 있다고 확신하지만, 나는 운없이 몇 시간 동안 이것에 대해 내 머리를 두들겨왔다. 나는 어떤 지침이라도 대단히 감사하겠습니다. 미리 감사드립니다.
나는 바보입니다. 요청을 정확히 구성한 방법이기 때문에 불투명 한 응답을 받고 있습니다.
이 문제에 대해 실행 한 모든 이유는 원래 요청을 시도 할 때 CORS 오류가 발생했기 때문입니다.
Access to fetch at 'https://login.microsoftonline.com/cattywampus.microsoftonline.com/oauth2/token' from origin 'https://localhost:44333' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
결과적 request.SetBrowserRequestMode(BrowserRequestMode.NoCors)
으로 불투명 한 응답이 결과에 관계없이 응답이 공백이라는 것을 이해하지 못하고 구성에 추가 했습니다.
분명히 이것은 나를 위해 아무것도 해결하지 못했습니다. 위의 오류 메시지에 대해 실행하는 경우 불투명 응답은 200도 어떤 종류의 페이로드도 반환하지 않는다는 것을 의미합니다. 그것이 불투명하다는 의미입니다. 적어도 나는 오늘 뭔가를 배웠다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다