我已经开发了Office.js Word加载项,并且正在使用here中所述的SSO功能。它使用Office.Auth interface
方法getAccessTokenAsync
,如图here所示。这可以按说明的方式工作,但是当我尝试使用更多范围使用Microsoft Graph访问SharePoint时,就会出现问题。
我无法成功进行Microsoft Graph调用,以使用从Office.Auth接口getAccessTokenAsync
方法生成的令牌访问SharePoint。
我已经完成了以下工作:
Sites.ReadWrite.All
和Sites.Manage.All
权限Azure AD应用程序注册。 [在Word加载项清单中添加了其他范围。
<WebApplicationInfo>
<Id>$application_GUID here$</Id>
<Resource>api://localhost:44355/$application_GUID here$</Resource>
<Scopes>
<Scope>Files.Read.All</Scope>
<Scope>Sites.ReadWrite.All</Scope>
<Scope>Sites.Manage.All</Scope>
<Scope>offline_access</Scope>
<Scope>openid</Scope>
<Scope>profile</Scope>
</Scopes>
</WebApplicationInfo>
使用图形资源管理器确认,我可以制作SharePoint图形以我的用户身份拨打电话。
GET https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items/{item-id}
Authorize
中为用户未登录时在here方法中添加了其他作用域。public async Task<ActionResult> Authorize()
{
ConfidentialClientApplicationBuilder clientBuilder = ConfidentialClientApplicationBuilder.Create(Settings.AzureADClientId);
clientBuilder.WithClientSecret(Settings.AzureADClientSecret);
clientBuilder.WithRedirectUri(loginRedirectUri.ToString());
clientBuilder.WithAuthority(Settings.AzureADAuthority);
ConfidentialClientApplication clientApp = (ConfidentialClientApplication)clientBuilder.Build();
string[] graphScopes = { "Files.Read.All", "User.Read", "Sites.ReadWrite.All", "Sites.Manage.All" };
// Get and save the token.
var authResultBuilder = clientApp.AcquireTokenByAuthorizationCode(
graphScopes,
Request.Params["code"] // The auth 'code' parameter from the Azure redirect.
);
try
{
var authResult = await authResultBuilder.ExecuteAsync();
ViewBag.AccessToken = authResult.AccessToken;
}
catch (Exception e)
{
ViewBag.Error = e.Message;
}
return View();
}
[当用户未登录Word且必须调用Authorize
方法时,我能够使用返回的令牌成功进行我的Graph调用。
[当用户已经登录到Word并且外接式JS使用getAccessTokenAsync
方法生成令牌时,尝试使用返回的令牌进行相同的Graph调用时,我得到了“未授权”。
我还需要为Office JS getAccessTokenAsync
方法做一些额外的事情,以返回授权的令牌以用于对SharePoint的Graph调用吗?
答案不多,但评论太久:
该示例最近更改为使用新的OfficeRuntime.auth.getAccessToken
。您似乎正在使用使用Office.auth.getAccessTokenAsync
的旧版本。那应该仍然有效,但是它是一个预览API,并且可能在某个时候停止工作。考虑克隆较新的版本。但是要等几天。我将对新版本进行一些修复。
检查是否已在Login操作方法中将新作用域添加到graphScopes
数组。 (除了像已经将它们添加到Authorize操作方法中一样。)
确切地说,哪个HTTP调用返回了“未经授权”?
正在从getAccessToken
(或getAccessTokenAsync
)重调出什么错误?它通常有一个13xxx号码。