νμ΄μ¬ 3μ λ¬Έμ κ° μμ΅λκΉ?
μλ νμλκΉ,
Python 2 λ° 3μ λ λ²μ λͺ¨λμ νΈνλμ΄μΌ νμ§λ§ νμΈνκ² μ΅λλ€. λΉμ μκ² λμμ¬ κ²μ λλ€.
μλ
νμΈμ μ μν λ΅λ³ κ°μ¬ν©λλ€.
μλλ λ΄κ° μ»λ μ€λ₯μ
λλ€
`---------------------------------------------------- --------------------------
ModuleNotFoundError μΆμ (κ°μ₯ μ΅κ·Ό νΈμΆ λ§μ§λ§)
1 κ°μ Έμ€κΈ° json
2
----> office365.runtime.auth.authentication_contextμμ 3 κ°μ Έμ€κΈ° AuthenticationContext
4 office365.runtime.client_request κ°μ Έμ€κΈ° ClientRequestμμ
5μμ office365.runtime.utilities.request_options κ°μ Έμ€κΈ° RequestOptions
ModuleNotFoundError: 'office365'λΌλ λͺ¨λμ΄ μμ΅λλ€.
κ·Έλ¦¬κ³ μ, pip install λͺ¨λμ΄ μμ΅λλ€. :)
μλ
νμΈμ, Requests λͺ¨λλ§μ μ¬μ©νμ¬ μ 보λ₯Ό μ»λ μ½λλ₯Ό μμ±νμ΅λλ€. FedAuth λ° rtFa μΏ ν€λ₯Ό μ±κ³΅μ μΌλ‘ κ°μ Έμ¬ μ μμ΅λλ€. κ·Έλ¬λ siteUsersλ₯Ό μ»μΌλ €κ³ ν λ Forbidden μ€λ₯κ° λ°μν©λλ€.
μ΄κ² μ’ λμμ£Όμκ² μ΄μ?
`κ°μ Έμ€κΈ° μμ²
url=" https://login.microsoftonline.com/extSTS.srf "
ν€λ = {'μ½ν
μΈ μ ν': 'μμ© νλ‘κ·Έλ¨/json'}
TopLevelUrl=' https://XXXX.sharepoint.com '
κ²½λ‘='C:\PSR\PythonFiles\SharePointOnlineXML.txt'
νμΌ λ΄μ© = μ΄κΈ°(κ²½λ‘, 'r')
SharepointOnlineAuth = filecontent.read()
SharepointOnlineAuth=SharepointOnlineAuth.format('[email protected]','λΉλ°λ²νΈ',TopLevelUrl)
μλ΅=requests.post(url,data=SharepointOnlineAuth,headers=headers)
s=str(μλ΅.λ΄μ©)
μμ = [λ²μ(len(s)μ posμ λν pos) if s[pos:].startswith('
Finish= [pos for pos in range(len(s)) if s[pos:].startswith('
LoginResponse=s[μμ+40:λ§μΉ¨]
url=TopLevelUrl+'/_forms/default.aspx?wa=wsignin1.0'
ν€λ = {'μ½ν
μΈ μ ν': 'μμ© νλ‘κ·Έλ¨/x-www-form-urlencoded'}
response=requests.post(url,data=LoginResponse,headers=headers)
url=TopLevelUrl+'/_api/web/siteusers'
μΈμ(URL)
_Fedauth='FedAuth={}'.format(response.cookies['FedAuth'])
_rtFa='rtFa={}'.format(response.cookies['rtFa'])
_FinalDict={'μΏ ν€':_Fedauth+','+_rtFa}
ν€λ = {'μ½ν
μΈ μ ν': 'μμ© νλ‘κ·Έλ¨/x-www-form-urlencoded',
'User-Agent':'Mozilla/5.0(Windows NT 10.0, Win64, x64) AppleWebKit/537.36(KHTML, Gecko λ±) Chrome/59.0.3071.115 Safari/537.36'}
headers.update(_FinalDict)
μλ΅=requests.get(URL, ν€λ=ν€λ)
μΈμ(μλ΅)`
python3.4μμ μ€λ₯κ° λ°μν©λλ€.
_Traceback(κ°μ₯ μ΅κ·Ό νΈμΆ λ§μ§λ§):
νμΌ "auth_sharepoint_online365.py", 2ν,
office365.runtime.auth.authentication_context κ°μ Έμ€κΈ°μμ AuthenticationContext κ°μ Έμ€κΈ°
νμΌ "authentication_context.py", 2ν,
office365.runtime.auth.saml_token_provider κ°μ Έμ€κΈ° SamlTokenProviderμμ
νμΌ "saml_token_provider.py", λΌμΈ 2,
urlparse κ°μ Έμ€κΈ°
ImportError: ' urlparse '_λΌλ λͺ¨λμ΄ μμ΅λλ€.
https://docs.python.org/2/library/urlparse.html μ λ°λ₯΄λ©΄ :
urlparse λͺ¨λμ Python 3μμ urllib.parseλ‘ μ΄λ¦μ΄ λ³κ²½λμμ΅λλ€.
μ΄ https://github.com/parthrawal/Python.O365λ₯Ό μλνμμμ€. SharePoint Onlineμ μ°κ²°ν μ μλ .py νμΌμ μμ±νμΌλ©° μ¬μ νμμ λͺ©λ‘μμ λ°μ΄ν°λ₯Ό κ°μ Έμ€λ λͺ κ°μ§ λ°©λ²λ μμ΅λλ€. λμμ΄ νμνλ©΄ μλ €μ£Όμμμ€.
@vic-by μ΅κ·Ό 컀λ°μΌλ‘ λ¬Έμ κ° ν΄κ²°λμμ΅λλ€. https://github.com/vgrem/Office365-REST-Python-Client/commit/90731f
νμ¬ μ΄ μ»€λ°μ μμ§ pypiμ νΈμλμ§ μμμ΅λλ€. λ°λΌμ pip install Office365-REST-Python-Client
λ₯Ό μννλ©΄ ν΄λΉ μ½λλ₯Ό μ»μ μ μμ΅λλ€. gitμμ μ€μΉ: pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git
python 3μμ λΌμ΄λΈλ¬λ¦¬λ₯Ό μ€μ λ‘ μ¬μ©νλ €λ©΄ μμ§ μμ μ΄ νμν©λλ€.
python 3μμ λΌμ΄λΈλ¬λ¦¬λ₯Ό μ€νν λ μΈμ½λ© λ¬Έμ κ° λ°μνμ΅λλ€. list.propertiesλ₯Ό μνν λ λ°μνμ§λ§ λ¬Έμ κ° μ§μμ μ΄λΌκ³ νμ ν©λλ€.
ctx = ClientContext(url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
lists = web.lists
ctx.load(lists)
ctx.execute_query()
lists[0].properties
in <module>()
---> 16 lists[0].properties
c:\python\python35\lib\encodings\cp850.py in encode(self, input, final)
17 class IncrementalEncoder(codecs.IncrementalEncoder):
18 def encode(self, input, final=False):
---> 19 return codecs.charmap_encode(input,self.errors,encoding_map)[0]
20
21 class IncrementalDecoder(codecs.IncrementalDecoder):
UnicodeEncodeError: 'charmap' codec can't encode character '\u2019' in position 11052: character maps to <undefined>
Python 3μμ λμΌν λ¬Έμ κ° μμ΅λλ€. "ModuleNotFoundError: 'urlparse'λΌλ λͺ¨λμ΄ μμ΅λλ€."
SharePointOnline.py νμΌ ν
μ€νΈ: μΈμ¦ μ€ν¨ -> μ
λ ₯νκ³ μ μ₯λ λΉλ°λ²νΈκ° μΌμΉνμ§ μμ΅λλ€.
μ΄ λ§ν¬λ₯Ό μλ νμ΅λκΉ https://github.com/parthrawal/Python.O365 λμΌν μλΉμ€ κ³μ μ μ¬μ©νλμ§ νμΈνμμμ€
μ. νλ¨μ μ½λ 볡μ¬/λΆμ¬λ£κΈ°:
if __name__ == "__main__":
UserName = "[email protected]"
# UserName = "[email protected]"
Password = "my_password"
TopLevelUrl = "https://company.sharepoint.com/"
SPOnlineHeaders(UserName, Password, TopLevelUrl)
λ€μ μ€λ₯κ° λ°νλ©λλ€.
....
IndexError: list index out of range
μ€λ₯μ λν μμΈν λ΄μ©μ νμΈνκΈ° μν΄ SPOnlineHeadersμ λ€μ μ½λ(print(s))λ₯Ό μ½μ νμ΅λλ€.
response = requests.post(url, data=SharepointOnlineAuth, headers=headers)
s = str(response.content)
print(s)
μλ΅:
... wst:FailedAuthentication... μ
λ ₯νκ³ μ μ₯λ λΉλ°λ²νΈκ° μΌμΉνμ§ μμ΅λλ€...
λλ λΉμ μ΄ κ·Έκ²μ μ¬μ©νλ κ²μ΄ μλΉμ€ κ³μ μ΄κΈ°λ₯Ό λ°λλλ€
λ§μμ. μλΉμ€ κ³μ μ μ¬μ©νκ³ μμ΅λλ€.
λΉλ°λ²νΈκ° λ§μ΅λκΉ? λ€μ νμΈν΄ μ£Όμκ² μ΅λκΉ? μ½λλ₯Ό μ€ννλ €κ³ νκΈ° λλ¬Έμ μ λλ‘ μλν©λλ€. κ·Έλ¬λ μλͺ»λ μνΈλ₯Ό μ λ ₯νλ©΄ "IndexError: list index out of range" μ€λ₯κ° λ°μν©λλ€.
μ. λΉλ°λ²νΈκ° μ νν©λλ€. μ΄κ²μ μλ λ‘κ·ΈμΈμ ν λ μΌμ΄λλ μΌμ λλ€.
λΈλΌμ°μ μ λ§ν¬λ₯Ό μλλ€. https://company.sharepoint.com/
Enter ν€λ₯Ό λλ₯΄λ©΄ λ‘κ·ΈμΈ νμ΄μ§λ‘ 리λλ μ λ©λλ€. https://login.microsoftonline.com/575...71a/oauth2/authorize?client_id=...&response_mode=form_post&response_type=code%20id_token&resource=...&scope=openid&nonce =....&redirect_uri=https :%2F%2Fcompany.sharepoint.com%2F_forms%2Fdefault.aspx&wsucxt=1&client-request-id=....
λ‘κ·ΈμΈ νμ΄μ§μμ νμΈ λ²νΌμ λλ₯Έ ν λ κ°μ λΉ λ₯Έ λ§ν¬κ° νμλμμ΅λλ€...
첫째: https://sts.company.com/adfs/ls/wia?client-request-id=...&wa=wsignin1.0&wtrealm=urn%3afederation%3aMicrosoftOnline&wctx=LoginOptions%3D3%26estsredirect%3d2%26estsrequest%. ...&cbcxt=&μ¬μ©μ μ΄λ¦=myemail%company.com&mkt=&lc=
... κ·Έλ¦¬κ³ ...
λ λ²μ§Έ: https://login.microsoftonline.com/login.srf?client-request-id=...
κ·Έλ¦¬κ³ λ§μ§λ§μΌλ‘ νμν νμ΄μ§μμ λ©μΆ₯λλ€.
https://company.sharepoint.com/
μ΄ μ½λλ₯Ό μ€ννλ©΄ μ½λ μ΄λκ°μ 403 μ€λ₯κ° λ°μνλ κ²μΌλ‘ λνλ¬μ΅λλ€.
μλ΅ = requests.post(url,data=SharepointOnlineAuth,headers=headers)
s = str(response.content)
μΈμλ¬Ό)
"b'403 ν¬λΉλ΄"
λμ μ μ¬μ©νλ κ²μ μμμ΅λλ€
urlpar
urlparse(Python 3.6.5 :: Anaconda, Inc.)μ λμΌν λ¬Έμ κ° μμ΅λλ€. μ΄κ²μ saml_token_provider.pyλ₯Ό μμ νμ¬ ν΄κ²°λμμ΅λλ€. from urllib.parse import urlparse [...] url = urlparse(self.url)
κ·Έλ° λ€μ pip install lpthw.web
μ ν¨κ» μ€μΉν μΉμ΄ λλ½λμμ΅λλ€. κ·Έλ¬λ μ§κΈ λ€μκ³Ό κ°μ λ¬Έμ κ° λ°μνλ―λ‘ λ¬Έμ κ° μλ κ² κ°μ΅λλ€.
from web import Web
File "C:\ProgramData\Anaconda3\lib\site-packages\web\__init__.py", line 14, in <module>
import utils, db, net, wsgi, http, webapi, httpserver, debugerror
ModuleNotFoundError: No module named 'utils'
μλμ½λ€μ utils λͺ¨λμ μ€μΉν©λλ€.
μλμ½λ€μ utils λͺ¨λμ μ€μΉν©λλ€.
κ°μ¬ ν΄μ. utilsλ₯Ό μ€μΉν ν dbλ μ€μΉν΄μΌ νμ΅λλ€... ν¨ν€μ§μμ requirements.txtλ₯Ό νμΈνλ κ²μ΄ μ μ©ν μ μμ΅λκΉ?
μ§κΈ μΌμ΄λκ³ μλ μΌμ print λ¬Έμ΄ python3μ μ ν©νμ§ μλ€λ κ²μ
λλ€.
print "var", var
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("var", var)?
λ€μ λ§ν¬λ₯Ό ν΅ν΄ μ΄λνμμμ€.
https://docs.python.org/3.0/whatsnew/3.0.html
@viogp μ§ν μν©μ μ¬κΈ°μ κ²μ
μλ
νμΈμ. μ΄ λΌμ΄λΈλ¬λ¦¬λ₯Ό μ¬μ©νλ λ° λ¬Έμ κ° μμ΅λλ€.
ν ν°μ κ²μνλ λμ μ€λ₯κ° λ°μνμ΅λλ€. AADSTS90023: STS μμ²μ΄ μλͺ»λμμ΅λλ€.
μΈμ¦ μΏ ν€λ₯Ό κ²μνλ λμ μ€λ₯κ° λ°μνμ΅λλ€.
λ°ν 'FedAuth=' + self.FedAuth + '; rtFa=' + self.rtFa
TypeError: str("NoneType" μλ)λ§ strμ μ°κ²°ν μ μμ΅λλ€.
μ무λ λλ₯Ό λμΈ μ μμ΅λκΉ?
λΌμ΄λΈλ¬λ¦¬λ 곡μμ μΌλ‘ Python 2 λ° Python 3 λ°νμκ³Ό νΈνλλ―λ‘ μ΄ λ¬Έμ λ₯Ό μ’ λ£ν κ²μ μ μν©λλ€.
μλ νμΈμ,
μ€ν μ€ pycharmμμ νλ‘μ νΈ νκ²½μ μ¬μ©ν λ "ModuleNotFoundError: No module named 'office365.sharepoint.client_context'" μ€λ₯κ° λ°μν©λλ€. κ·Έλ¬λ Python μμ€ν νκ²½μ μ¬μ©νλ©΄ μ μλν©λλ€. λμΌν λμμ Python 3.6 λλ 3.7μ μ¬μ©ν©λλ€.
λμμ£ΌμΈμ!
κ°μ¬ ν΄μ.
κ°μ₯ μ μ©ν λκΈ
@vic-by μ΅κ·Ό 컀λ°μΌλ‘ λ¬Έμ κ° ν΄κ²°λμμ΅λλ€. https://github.com/vgrem/Office365-REST-Python-Client/commit/90731f
νμ¬ μ΄ μ»€λ°μ μμ§ pypiμ νΈμλμ§ μμμ΅λλ€. λ°λΌμ
pip install Office365-REST-Python-Client
λ₯Ό μννλ©΄ ν΄λΉ μ½λλ₯Ό μ»μ μ μμ΅λλ€. gitμμ μ€μΉ:pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git