現在Office365-REST-Python-Client==2.3.0.1
次のコードを実行すると:
def upload_to_sharepoint():
url = 'https://tenant.sharepoint.com/sites/sitename'
credentials = {
'client_id': os.environ.get('client_id'),
'client_secret': os.environ.get('client_secret'),
}
context_auth = AuthenticationContext(url=url)
context_auth.acquire_token_for_app(client_id=credentials['client_id'],
client_secret=credentials['client_secret'])
ctx = ClientContext(url, context_auth)
web = ctx.web
ctx.load(web)
file_name = f"vat check {datetime.now().strftime('%Y-%m-%d')}"
path = os.path.join("/tmp", file_name)
with open(path, 'rb') as content_file:
file_content = content_file.read()
list_title = "VAT Check"
target_folder = ctx.web.lists.get_by_title(list_title).rootFolder
# name = os.path.basename(path)
target_file = target_folder.upload_file(file_name, file_content)
ctx.execute_query()
print(f"File url: {target_file.serverRelativeUrl}")
次のエラーが発生します。これは、認証に関するものではなく、内部的なエラーのようです。または、何かが足りませんか?
AttributeError: 'ClientContext' object has no attribute '_auth_context'
問題を解決し、ローカルでテストした@vgremに感謝します。
ご挨拶、
キャッチしてくれてありがとう! ClientContext
に渡すときに、 AuthenticationContext
のインスタンスが失われたことが判明しました。
一方、 AuthenticationContext.acquire_token_for_app
は現在_非推奨_と見なされているため、別のオプション(_recommended_)は次のように切り替えることです。
context_auth = AuthenticationContext(url=url)
context_auth.acquire_token_for_app(client_id=credentials['client_id'],
client_secret=credentials['client_secret'])
ctx = ClientContext(url, context_auth)
に
credentials = ClientCredential(credentials['client_id'], credentials['client_secret'])
ctx = ClientContext(settings['url']).with_credentials(credentials)
私はまだこの問題を抱えています。
何が理由でしょうか
おそらくパッケージを更新する必要があります: pip install --upgrade Office365-REST-Python-Client
@spurthikaribasaiah
こんにちは@erfannarimanと@vgrem 、
上記のコマンドで試してみました。 私はまだ同じ問題を抱えています。
以下のコードスニペットを見つけてください。 ユーザーの資格情報を使用しています
pip install --upgrade pip
pip installOffice365-REST-Python-クライアント
pip install --upgrade Office365-REST-Python-Client
office365.runtime.auth.authentication_contextからimportAuthenticationContext
office365.sharepoint.client_contextからimportClientContext
ctx_auth = AuthenticationContext(Sharepoint_SiteUrl)
ctx_auth.acquire_token_for_user(User、Pass)
ctx = ClientContext(Sharepoint_SiteUrl、ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print( "認証に成功しました")