正如问题所言,我已经创建了一个简单的asp.net mvc项目,其中有邮递员进行练习。我从official website开始学习了所有教程。我想做的程序是修改邮箱设置。
这是我尝试的代码
private const string clientId = "*******";
private const string clientKey = "********";
private const string tenant = "********";
private const string instance = "https://login.microsoftonline.com/";
private const string developer = "*******";
[HttpPatch]
public async Task<IHttpActionResult> TEST()
{
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenant)
.WithClientSecret(clientKey)
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var mailboxSettings = new MailboxSettings
{
AdditionalData = new Dictionary<string, object>()
{
{"@odata.context","https://graph.microsoft.com/beta/" + developer + "/mailboxSettings"}
},
AutomaticRepliesSetting = new AutomaticRepliesSetting
{
Status = AutomaticRepliesStatus.Scheduled,
ScheduledStartDateTime = new DateTimeTimeZone
{
DateTime = "2016-03-20T18:00:00",
TimeZone = "UTC"
},
ScheduledEndDateTime = new DateTimeTimeZone
{
DateTime = "2016-03-28T18:00:00",
TimeZone = "UTC"
}
}
};
var me = new User();
me.MailboxSettings = mailboxSettings;
await graphClient.Me
.Request()
.UpdateAsync(me);
if (me.MailboxSettings != null)
{
return Ok(new ApiResponseBase<List<OooRulesResponse>>()
{
Data = null,
Message = "Success",
Status = ApiResponseBaseStatus.Success
});
}
else
{
return Ok(new ApiResponseBase<List<OooRulesResponse>>()
{
Data = null,
Message = "Failed to insert scheduler.",
Status = ApiResponseBaseStatus.Success
});
}
}
邮递员告诉我这个错误
Message: Current authenticated context is not valid for this request. This occurs when a request is made
to an endpoint that requires user sign-in. For example, /me requires a signed-in user. Acquire a token
on behalf of a user to make requests to these endpoints. Use the OAuth 2.0 authorization code flow for
mobile and native apps and the OAuth 2.0 implicit flow for single-page web apps.
是的,我知道可以选择一些身份验证提供程序来获取令牌,因此我使用Daemon应用程序和客户端凭据提供程序来构建它。在编写代码之前,我还设置了一个环境,该环境设置为MailboxSettings.ReadWrite等在没有用户的情况下获得访问权限。
但是此错误表明我正在使用另一种方法,该方法代表用户获取访问权限。我该如何解决这个错误?