使用Microsoft Graph 1.0从另一个用户/站点(不是我所有,但与我共享,可以通过Web访问)访问列表时出现问题?

时间:2019-10-17 14:49:13

标签: python sharepoint microsoft-graph

我已经检查了问题:Is there any way we can access the `shared with me` files in graph API Microsoft using application permission?,因为它与我的问题相当接近,但还不够严格,因此提出了新的问题。

我有一个由组织的另一个成员与我共享的列表,我可以通过浏览器打开和访问该列表。

让我们说一下URL是:https://365.sharepoint.com/teams/IC/Lists/Cult%20Tracker/Allitemsg.aspx#InplviewHash7c06d6bd-0344-43fd-8057-ffc7deeec3f7=ShowInGrid%3DTrue

现在,我正在尝试通过python提取此数据并对其进行一些分析。 因此,我尝试使用“ sharepoint”和“ Office365-REST-Python-Client”软件包,但两者均无效,并不断抛出403。

我尝试过的代码是:

from sharepoint import SharePointSite, basic_auth_opener

server_url = "https://<host>365.sharepoint.com/"
site_url = server_url + "teams/IC"

opener = basic_auth_opener(server_url, "slaik@<host>.com", "Abcdefgh")
# also tried domain/slaik as userid

site = SharePointSite(site_url, opener)

for sp_list in site.lists:
    print(sp_list.id, sp_list.meta['Title'])

我得到的错误:

Traceback (most recent call last):
  File "C:/Users/slaik/OneDrive - <host>/Migrated from My PC/Documents/Sandeep/Scripts/Python/PycharmProjects/IC_KPI_Report/sharepoint_conect.py", line 10, in <module>
    for sp_list in site.lists:
  File "C:\Users\slaik\OneDrive - <host>\Migrated from My PC\Documents\Sandeep\Scripts\Python\PycharmProjects\IC_KPI_Report\venv\lib\site-packages\sharepoint\lists\__init__.py", line 80, in __iter__
    return iter(self.all_lists)
  File "C:\Users\slaik\OneDrive - <host>\Migrated from My PC\Documents\Sandeep\Scripts\Python\PycharmProjects\IC_KPI_Report\venv\lib\site-packages\sharepoint\lists\__init__.py", line 36, in all_lists
    result = self.opener.post_soap(LIST_WEBSERVICE, xml)
  File "C:\Users\slaik\OneDrive - <host>\Migrated from My PC\Documents\Sandeep\Scripts\Python\PycharmProjects\IC_KPI_Report\venv\lib\site-packages\sharepoint\site.py", line 32, in post_soap
    response = self.opener.open(request, timeout=self.timeout)
  File "c:\users\slaik\appdata\local\programs\python\python36\Lib\urllib\request.py", line 532, in open
    response = meth(req, response)
  File "c:\users\slaik\appdata\local\programs\python\python36\Lib\urllib\request.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "c:\users\slaik\appdata\local\programs\python\python36\Lib\urllib\request.py", line 570, in error
    return self._call_chain(*args)
  File "c:\users\slaik\appdata\local\programs\python\python36\Lib\urllib\request.py", line 504, in _call_chain
    result = func(*args)
  File "c:\users\slaik\appdata\local\programs\python\python36\Lib\urllib\request.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

import json

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.client_request import ClientRequest
from office365.runtime.utilities.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext

url = "https://<host>365.sharepoint.com/teams/IC"
username = "slaik@<host>.com" #also tried domain/slaik as userid
password = "Abcdefgh"

ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
  request = ClientRequest(ctx_auth)
  options = RequestOptions("{0}/_api/web/".format(url))
  options.set_header('Accept', 'application/json')
  options.set_header('Content-Type', 'application/json')
  data = request.execute_request_direct(options)
  s = json.loads(data.content)
  web_title = s['Title']
  print("Web title: " + web_title)
else:
  print(ctx_auth.get_last_error())

我得到的错误:

An error occurred while retrieving token: AADSTS50126: Invalid username or password.
An error occurred while retrieving auth cookies
Traceback (most recent call last):
  File "C:/Users/slaik/OneDrive - <host>/Migrated from My PC/Documents/Sandeep/Scripts/Python/PycharmProjects/IC_KPI_Report/sharepoint_conect.py", line 30, in <module>
    data = request.execute_request_direct(options)
  File "C:\Users\slaik\OneDrive - <host>\Migrated from My PC\Documents\Sandeep\Scripts\Python\PycharmProjects\IC_KPI_Report\venv\lib\site-packages\office365\runtime\client_request.py", line 104, in execute_request_direct
    self.context.authenticate_request(request_options)
  File "C:\Users\slaik\OneDrive - <host>\Migrated from My PC\Documents\Sandeep\Scripts\Python\PycharmProjects\IC_KPI_Report\venv\lib\site-packages\office365\runtime\auth\authentication_context.py", line 35, in authenticate_request
    request_options.set_header('Cookie', self.provider.get_authentication_cookie())
  File "C:\Users\slaik\OneDrive - <host>\Migrated from My PC\Documents\Sandeep\Scripts\Python\PycharmProjects\IC_KPI_Report\venv\lib\site-packages\office365\runtime\auth\saml_token_provider.py", line 69, in get_authentication_cookie
    return 'FedAuth=' + self.FedAuth + '; rtFa=' + self.rtFa
TypeError: must be str, not NoneType

在堆栈溢出的一个答案中,我知道它不再能那样工作了,我应该使用Microsoft graph api。

因此,我登录了api控制台,并向其提供了Sites.Read.All,Sites.ReadWrite.All和其他权限。

因此,现在,如果我尝试这样做:获取https://graph.microsoft.com/v1.0/sites?search=IC

我收到200条回复

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites",
    "value": [
        {
            "createdDateTime": "2017-05-26T14:04:29Z",
            "id": "<host>365.sharepoint.com,fec353b1-bd1a-4569-acd9-6421737b81d6,5b449746-eaae-4941-b044-ef9703a6b2ee",
            "lastModifiedDateTime": "0001-01-01T08:00:00Z",
            "name": "IC",
            "webUrl": "https://<host>365.sharepoint.com/teams/IC",
            "displayName": "IC Division - MRC",
            "root": {},
            "siteCollection": {
                "hostname": "<host>365.sharepoint.com"
            }
        }
    ]
}

但是,如果我尝试这样做,请获取https://graph.microsoft.com/v1.0/sites/IC/lists

或者这个:https://graph.microsoft.com/v1.0/sites/IC/lists/Cult%20Tracker

我得到一个错误:

{
    "error": {
        "code": "invalidRequest",
        "message": "Invalid hostname for this tenancy",
        "innerError": {
            "request-id": "ab43af0a-6be0-4a40-91ab-5a4ff52bc261",
            "date": "2019-10-17T14:05:52"
        }
    }
}

我指的是文档:https://docs.microsoft.com/en-us/graph/api/resources/list?view=graph-rest-1.0 不确定我想念什么。

感谢您阅读这份详细说明以及您可以提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

您需要使用站点ID like this

GET /sites/{site-id}

换句话说,您的URI应该是

https://graph.microsoft.com/v1.0/sites/host>365.sharepoint.com,fec353b1-bd1a-4569-acd9-6421737b81d6,5b449746-eaae-4941-b044-ef9703a6b2ee/lists