How to use Python to access a SharePoint excel sheet having MFA enabled for AWS lambda function

Viewed 295

How to use Python to access a Sharepoint excel sheet with having MFA enabled for AWS lambda function.

Previously I have used the following command

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import xlrd
import io
import pandas as pd
import urllib3
from urllib.request import urlopen
import os, tempfile, zipfile
import boto3
from base64 import b64decode
import json
ENCRYPTED_USER = os.environ['user']
ENCRYPTED_PASS = os.environ['pass']
# Decrypt code should run once and variables stored outside of the function
# handler so that these are decrypted once per container
DECRYPTED_USER = boto3.client('kms').decrypt(
    CiphertextBlob=b64decode(ENCRYPTED_USER),
    EncryptionContext={'LambdaFunctionName': os.environ['AWS_LAMBDA_FUNCTION_NAME']}
)['Plaintext'].decode('utf-8')
DECRYPTED_PASS = boto3.client('kms').decrypt(
    CiphertextBlob=b64decode(ENCRYPTED_PASS),
    EncryptionContext={'LambdaFunctionName': os.environ['AWS_LAMBDA_FUNCTION_NAME']}
)['Plaintext'].decode('utf-8')
def lambda_handler(event, context):
    http = urllib3.PoolManager()
    #Todays date
    from datetime import date
    today = date.today()
    d2 = today.strftime("%B %d")
    month = today.strftime("%B")
    #target url taken from sharepoint and credentials
    url = 'https://epiuse-my.sharepoint.com/:x:/r/personal/202021.xlsx?'
    username = DECRYPTED_USER
    password = DECRYPTED_PASS

But now I have enabled MFA for my mail id, and started to get errors. Is there any way to resolve the issue

ERROR:

{
  "errorMessage": "An error occurred while retrieving token from XML response: AADSTS50076: Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access ''.",
  "errorType": "ValueError",
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      38,
      "lambda_handler",
      "if ctx_auth.acquire_token_for_user(username, password):"
    ],
    [
      "/var/task/office365/runtime/auth/authentication_context.py",
      72,
      "acquire_token_for_user",
      "return self._provider.ensure_authentication_cookie()"
    ],
    [
      "/var/task/office365/runtime/auth/providers/saml_token_provider.py",
      80,
      "ensure_authentication_cookie",
      "self._cached_auth_cookies = self.get_authentication_cookie()"
    ],
    [
      "/var/task/office365/runtime/auth/providers/saml_token_provider.py",
      95,
      "get_authentication_cookie",
      "token = self._acquire_service_token()"
    ],
    [
      "/var/task/office365/runtime/auth/providers/saml_token_provider.py",
      172,
      "_acquire_service_token",
      "token = self._process_service_token_response(response)"
    ],
    [
      "/var/task/office365/runtime/auth/providers/saml_token_provider.py",
      197,
      "_process_service_token_response",
      "raise ValueError(self.error)"
    ]
  ]
}

Please help me to troubleshoot this issue!!!

0 Answers
Related