Код: Выделить всё
private static async Task GetEmailMessageAsync(MailboxItemModel mailboxItem) =>
await GetGraphClient(mailboxItem).Me.Messages[mailboxItem.ItemId].Content.GetAsync();
private static GraphServiceClient GetGraphClient(MailboxItemModel mailboxItem) =>
new GraphServiceClient(new ExistingTokenProvider(mailboxItem.Token));
private sealed class ExistingTokenProvider : IAuthenticationProvider {
private readonly string _token;
public ExistingTokenProvider(string token) => _token = token;
public Task AuthenticateRequestAsync(
RequestInformation request,
Dictionary additionalAuthenticationContext = null,
CancellationToken cancellationToken = new CancellationToken()) {
request.Headers["Authorization"] = new[] { "Bearer " + _token };
return Task.CompletedTask;
}
}
Код: Выделить всё
mailboxItem.ItemId
Код: Выделить всё
/** Public Client Application for getting tokens.
* @type {import('@azure/msal-browser').IPublicClientApplication}
* @readonly
*/
var _pca = undefined;
var initialiseNAA = function() {
var clientId = ViewState.GetMsalClientId();
console.log("Creating NPCA for client " + clientId + "...");
msal.createNestablePublicClientApplication({ auth: { clientId: clientId } }).then(
function(pca) {
_pca = pca;
console.log("Created NPCA for client " + clientId + ".");
},
function(error) {
console.log("Failed to create NPCA for client " + clientId + ": " + JSON.stringify(error));
});
};
/** Get Exchange data for the selected mailbox item.
* @param {getMailboxItemOnSuccess} onSuccess Continuation to run when the data has been read.
*/
var getMailboxItem = function(onSuccess) {
console.log("Getting Exchange data...");
var itemId = Office.context.mailbox.item.itemId;
console.log("itemId: " + itemId);
var afterSave = function() {
var request = { scopes: ViewState.GetMsalScope().split(" ") };
console.log("Acquiring PCA token silently...");
_pca.acquireTokenSilent(request).then(
function(result) {
console.log("Acquired PCA token silently.");
//console.log("graphToken: " + result.accessToken);
onSuccess({ url: _graphUrl, token: result.accessToken, itemId: itemId });
},
function(error1) {
console.log("Failed to acquire PCA token silently: " + JSON.stringify(error1));
if(error1 instanceof msal.InteractionRequiredAuthError) {
console.log("Acquiring PCA token interactively...");
_pca.acquireTokenPopup(request).then(
function(result) {
console.log("Acquired PCA token interactively.");
//console.log("graphToken: " + result.accessToken);
onSuccess({ url: _graphUrl, token: result.accessToken, itemId: itemId });
},
function(error2) {
console.log("Failed to acquire PCA token interactively: " + JSON.stringify(error2));
_handleOfficeError(error2, "Cannot get a PCA token to read the mailbox item.");
}
);
} else {
_handleOfficeError(error1, "Cannot get a PCA token to read the mailbox item.");
}
}
);
};
if(itemId) {
// We have the item ID already, it's safe to continue.
afterSave();
} else {
// We must save the item first before we can read data.
console.log("Saving the item first...");
Office.context.mailbox.item.saveAsync(function(saveAsyncResult) {
if(saveAsyncResult.status !== Office.AsyncResultStatus.Succeeded) {
_handleOfficeError(saveAsyncResult, "Cannot save a draft of the mailbox item.");
return;
}
itemId = saveAsyncResult.value;
console.log("itemId: " + itemId);
afterSave();
});
}
};
< /code>
Manifest xml настроен для использования вложенной аутентификации приложения (NAA): < /p>
MY_CLIENT_ID
api://MY_SITE/MY_CLIENT_ID
Mail.Read
Mail.Read.Shared
< /code>
MY_CLIENT_ID
Message: Resource not found for the segment '5NET4'.
AdditionalData:
Error: {"AdditionalData":{},"BackingStore":{"ReturnOnlyChangedValues":false,"InitializationCompleted":true},"Code":"RequestBroker--ParseUri","Details":null,"InnerError":null,"Message":"Resource not found for the segment '5NET4'.","Target":null}
ResponseStatusCode: 400
ResponseHeaders:
Cache-Control: private
Date: Mon, 11 Aug 2025 07:29:44 GMT
strict-transport-security: max-age=31536000
request-id: 6a6dda61-78ce-4ce9-a3c1-8c668953f9b3
client-request-id: 14f88e88-c493-4095-b5f6-56159ab0de6e
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"North Europe","Slice":"E","Ring":"4","ScaleUnit":"005","RoleInstance":"DU6PEPF00021B6F"}}
HResult: 0x80131500
Source: Microsoft.Kiota.Http.HttpClientLibrary
TargetSite: Void MoveNext()
StackTrace:
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.d__28.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.d__21`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.d__21`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Graph.Me.Messages.Item.Value.ContentRequestBuilder.d__3.MoveNext()
< /code>
The segment name is a random alphanumeric string that means nothing to us, and changes each time an error is logged.
For one user, I noticed that their Office licence location was a different country to the rest of us. I changed it to match the rest of us, and that made the error go away. However, another user is getting the error even with the correct country.
Can anyone tell me the root cause of this error?
UPDATE
User A was getting the error all morning, but is now working fine. Nothing has changed.
User B is still not working.
My user has worked all along.
Подробнее здесь: https://stackoverflow.com/questions/797 ... -graph-api