如何在python中访问SharePoint在线文档

时间:2019-04-30 06:43:04

标签: python-2.7 sharepoint-online

我已经在Microsoft门户https://apps.dev.microsoft.com上注册了一个应用程序,我具有客户端ID和客户端密码。 我正在使用Office365-REST-Python-Client。现在,我想使用公司用户名访问文档。 username@companyname.com

我正在使用以下代码:

from office365.runtime.auth.authentication_context import 
AuthenticationContext
from office365.sharepoint.client_context import ClientContext

app_settings = {
    'url': 'https://companyname.sharepoint.com',
    'client_id': '',
    'client_secret': '',
}

context_auth = AuthenticationContext(url=app_settings['url'])
context_auth.acquire_token_for_app(client_id=app_settings['client_id'], 
client_secret=app_settings['client_secret'])

ctx = ClientContext(app_settings['url'], context_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web site title: {0}".format(web.properties['Title']))

并且我遇到以下错误:

Traceback (most recent call last):
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\client_request.py", line 143, in validate_response
    response.raise_for_status()
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\requests\models.py", line 851, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".\auth_sharepointonline.py", line 16, in <module>
    ctx.execute_query()
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\client_runtime_context.py", line 33, in execute_query
    self.pending_request.execute_query()
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\client_request.py", line 28, in execute_query
    return self.execute_pending_queries()
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\client_request.py", line 35, in execute_pending_queries
    self.process_payload_json(query, response)
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\client_request.py", line 46, in process_payload_json
    self.validate_response(response)
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\client_request.py", line 145, in validate_response
    raise ClientRequestException(*e.args, response=e.response)
office365.runtime.client_request_exception.ClientRequestException: (None, None, '403 Client Error: Forbidden')

当我在内部公司网络中时,我曾尝试使用外部网络并出现以下错误:

  File "auth_sharepointonline.py", line 19, in <module>
    ctx.execute_query()
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\client_runtime_context.py", line 33, in execute_query
    self.pending_request.execute_query()
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\client_request.py", line 28, in execute_query
    return self.execute_pending_queries()
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\client_request.py", line 34, in execute_pending_queries
    response = self.execute_request_direct(request)
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\client_request.py", line 101, in execute_request_direct
    self.context.authenticate_request(request_options)
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\client_runtime_context.py", line 16, in authenticate_request
    self.__auth_context.authenticate_request(request)
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\auth\authentication_context.py", line 29, in authenticate_request
    request_options.set_header('Authorization', self.provider.get_authorization_header())
  File "C:\Users\msarpal\AppData\Local\Continuum\anaconda3\lib\site-packages\office365\runtime\auth\acs_token_provider.py", line 76, in get_authorization_header
    return 'Bearer {0}'.format(self.access_token["access_token"])
TypeError: 'NoneType' object is not subscriptable

0 个答案:

没有答案