与访问使用NTLM SharePoint列表当前用户凭据Python脚本是否授权 [英] Authorise with current user credentials for Python script accessing SharePoint list using NTLM

查看:881
本文介绍了与访问使用NTLM SharePoint列表当前用户凭据Python脚本是否授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本来检查特定文件修订版SharePoint列表,并返回结果。这种运作良好,但目前我的授权方法需要我包括在code我自己的密码来访问SharePoint列表,这是不理想的因为密码必须经常更新和其他用户将有可能查看我的登录详细信息。

有人能指出我在正确的方向使用当前用户的凭据访问SharePoint网站?我一直转圈圈看着ActiveDirectory中,NTLM,SOAP等,并无法破译其中的哪些是最合适的方法。

我使用Python 2.7和工作功能如下:

 进口的urllib2
从SharePoint进口SharePointSite
从NTLM进口HTTPNtlmAuthHandler高清read_sharepoint_list(current_project):
    #我的Windows凭据
    用户名=域名\\\\ user.name
    密码=MY_PASSWORD
    #在SharePoint信息
    SITE_URL =HTTP://的SharePoint /
    LIST_NAME =我的列表
    #为NTLM身份验证的揭幕战
    帕斯曼= urllib2.HTTPPasswordMgrWithDefaultRealm()
    passman.add_password(无,SITE_URL,用户名,密码)
    auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(帕斯曼)
    #创建和安装开门红
    首战= urllib2.build_opener(auth_NTLM)
    urllib2.install_opener(揭幕战)
    #创建一个SharePointSite对象
    网站= SharePointSite(SITE_URL,开瓶器)
    sp_list = site.lists [LIST_NAME]    在sp_list.rows行:
        如果current_project在row.Filename:
            basecase_rev = row.Rev    返回basecase_rev


解决方案

有对于requests_ntlm库的这里在SSPI身份验证合并为Windows用户。我不得不做出一些修改到code为它是功能性的,但它是为我工作。

您需要先安装要求和requests_ntlm,然后修改requests_ntlm \\ __ init__.py包文件(在你的PythonLIB \\站点包,如果在Windows文件夹中)类似于以下内容:

 从.requests_ntlm进口HttpNtlmAuth
从.requests_ntlmsspi进口HttpNtlmSspiAuth__all__ =('HttpNtlmAuth','HttpNtlmSspiAuth')

接下来,(从上面的链接)添加requests_ntlmsspi.py文件到requests_ntlm包文件夹。

然后,您应该可以使用当前用户的凭据进行身份验证如下:

 进口要求
从requests_ntlm进口HttpNtlmAuth,HttpNtlmSspiAuthrequests.get(SITE_URL,AUTH = HttpNtlmSspiAuth())

I have a script that checks a SharePoint list for a specific file revision and returns the result. This works well, but currently my authorisation method requires me to include my own password in the code to gain access to the SharePoint list, which isn't ideal given that passwords have to be updated frequently and other users can potentially view my login details.

Can someone point me in the right direction for using the current users credentials to access the SharePoint site? I've been going round in circles looking at ActiveDirectory, NTLM, SOAP etc and cannot decipher which of these is the most appropriate method.

I am using Python 2.7 and the working function is as follows:

import urllib2
from sharepoint import SharePointSite
from ntlm import HTTPNtlmAuthHandler

def read_sharepoint_list(current_project):
    # my Windows credentials
    username = "Domain\\user.name"
    password = "my_password"
    # the sharepoint info
    site_url = "http://SharePoint/"
    list_name = "My List"
    # an opener for the NTLM authentication
    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
    passman.add_password(None, site_url, username, password)
    auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
    # create and install the opener
    opener = urllib2.build_opener(auth_NTLM)
    urllib2.install_opener(opener)
    # create a SharePointSite object
    site = SharePointSite(site_url, opener)
    sp_list = site.lists[list_name] 

    for row in sp_list.rows:
        if current_project in row.Filename:
            basecase_rev = row.Rev

    return basecase_rev

解决方案

There is an open pull request for the requests_ntlm library here to merge in SSPI authentication for Windows users. I had to make a few edits to the code for it to be functional, but it is working for me.

You'll first need to install requests and requests_ntlm, then modify the "requests_ntlm\__init__.py" package file (in your Python "Lib\site-packages" folder if on Windows) to resemble the following:

from .requests_ntlm import HttpNtlmAuth
from .requests_ntlmsspi import HttpNtlmSspiAuth

__all__ = ('HttpNtlmAuth', 'HttpNtlmSspiAuth')

Next, add the "requests_ntlmsspi.py" file (from the link above) to the "requests_ntlm" package folder.

You should then be able to authenticate using the current user's credentials as follows:

import requests
from requests_ntlm import HttpNtlmAuth, HttpNtlmSspiAuth

requests.get(site_url, auth=HttpNtlmSspiAuth())

这篇关于与访问使用NTLM SharePoint列表当前用户凭据Python脚本是否授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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