我使用.net6进行身份验证并读取响应,但似乎什么都不起作用,我不知道我的身份验证是否正确,更不用说我的代码是否正确了
Azure中的所有配置都是根据文档完成的,我尝试了几种方法,但我不能成功,这是我最后一次尝试使它工作的代码
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
namespace MicrosoftFormsExample
{
class Program
{
static async Task Main(string[] args)
{
var responses = await GetFormResponses();
PrintResponses(responses);
}
private static async Task<IList<FormResponse>> GetFormResponses()
{
var clientId = "<your-client-id>";
var tenantId = "<your-tenant-id>";
var clientSecret = "<your-client-secret>";
var formId = "<form-id>";
var token = await GetToken(clientId, tenantId, clientSecret);
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await httpClient.GetAsync($"https://graph.microsoft.com/v1.0/forms/{formId}/responses");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var formResponses = JsonSerializer.Deserialize<FormResponseCollection>(content, options).Value;
return formResponses;
}
private static async Task<string> GetToken(string clientId, string tenantId, string clientSecret)
{
using var httpClient = new HttpClient();
var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"client_id", clientId},
{"scope", "https://graph.microsoft.com/.default"},
{"client_secret", clientSecret},
{"grant_type", "client_credentials"}
});
var response = await httpClient.PostAsync($"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token", content);
response.EnsureSuccessStatusCode();
var tokenResponse = await response.Content.ReadAsStringAsync();
var token = JsonSerializer.Deserialize<TokenResponse>(tokenResponse).AccessToken;
return token;
}
private static void PrintResponses(IList<FormResponse> responses)
{
foreach (var response in responses)
{
Console.WriteLine($"Response ID: {response.Id}");
Console.WriteLine($"Submitted Date and Time: {response.SubmittedDateTime}");
Console.WriteLine("Answers:");
foreach (var question in response.Questions)
{
Console.WriteLine($" {question.QuestionText}: {question.ResponseText}");
}
Console.WriteLine();
}
}
}
class FormResponseCollection
{
public IList<FormResponse> Value { get; set; }
}
class FormResponse
{
public string Id { get; set; }
public DateTimeOffset SubmittedDateTime { get; set; }
public IList<QuestionResponse> Questions { get; set; }
}
class QuestionResponse
{
public string QuestionText { get; set; }
public string ResponseText { get; set; }
}
class TokenResponse
{
public string AccessToken { get; set; }
}
}
你能帮我吗?
1条答案
按热度按时间mwngjboj1#
这种集成不工作,我最终不得不使用Power Automate集成,在它我转换的答案列表,并通过此代码,我可以阅读答案,并删除它们