在向列表中的项目询问sharepoint时发生UnauthorizedAccessException [英] UnauthorizedAccessException when asking sharepoint for items in a list

查看:62
本文介绍了在向列表中的项目询问sharepoint时发生UnauthorizedAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试访问sharepoint站点中列表的项目。我在页面上创建了一个clientId和一个secretId"appregnew.aspx"。然后我在页面"appinv.aspx"上创建访问规则。在这个页面上,我设置了访问规则
的XML代码,如:

I am trying to access list's items in my sharepoint site. I have created a clientId and a secretId on the page "appregnew.aspx". Then I create the access rule on the page "appinv.aspx". On this page, I set the XML code for the access rule like is :

<AppPermissionRequests><AppPermissionRequest 
   Scope="http://sharepoint/content/sitecollection" 
   Right="FullControl"/></AppPermissionRequests>



我创建规则并在下一页确认。

I create the rule and confirm it on the next page.


在我的应用程序中,我使用此代码获取令牌

In my app, I get the token with this code

let payload = `grant_type=client_credentials
            &client_id=${clientId}@${tenantId}
            &client_secret=${clientSecret}
&resource=${resource}/${domain}@${tenantId}`;
        let authOptions = {
            hostname: 'accounts.accesscontrol.windows.net',
            path: `/${tenantId}/tokens/OAuth/2`,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': Buffer.byteLength(payload)
            }
        };
        let req = https.request(authOptions, res => {...})


then I do this to get the items in my list :

let listOptions = {
        hostname: 'XXXXX.sharepoint.com',
        path: '/sites/XXX/_api/lists/getbytitle('MyList')/items',
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `bearer ${accessToken}`,
            'Accept': 'application/json'
        }
    };
    let req = https.request(listOptions, res => {
        res.setEncoding('utf8');
        let stringResult = '';
        res.on('data', chunk => {
            stringResult += chunk.toString();
        });



Sharepoint总是返回此错误

Sharepoint always return this error


"{" error":{" code":" -2147024891,System.UnauthorizedAccessException"," message":{" lang":" en-US" ,"值":"拒绝访问。您无权执行此操作或访问
此资源。"}}}"

"{"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied. You do not have permission to perform this action or access this resource."}}}"


我做错了什么?

What do I do wrong ?


非常感谢您的帮助。

Thanks a lot for your help.


Gilles

Gilles

推荐答案

嗨DGiles,

Hi DGiles,

如果您在SharePoint应用程序中获取列表项,则需要使用跨域库并在AppManifest.xml中设置应用程序权限(如此演示),而不需要在代码中获取令牌:

If you are get list items in SharePoint app, you will need to use Cross Domain library and set app permission in AppManifest.xml like this demo and not need to fetch token in the code:

Cross使用REST的域和SharePoint托管应用程序

谢谢

最好的问候


这篇关于在向列表中的项目询问sharepoint时发生UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆