作为数据工程工作的一部分,我不久前继承了一些用Python编写的Azure函数。我现在必须更新它们以反映更改后的凭据,但仍然遇到问题。
部署时,它会触发以下“发送正文时需要双工选项”。
我在 SO 和其他网站上发现了许多建议添加“duplex”:“half”或“duplex true”的帖子,并且它暗示以某种方式与 node.js 相关(据我所知,我们不使用)。我还偶然发现了一个 SO 线程,表明该错误可能是由于请求的令牌未及时通过并使调用异步引起的。
我对 C# 很熟悉,但对 Python 非常陌生,所以我希望有人可以看看代码并让我知道我可以在什么/哪里解决这个问题,因为到目前为止我的尝试没有产生任何结果。
我们的代码:
import logging
import pandas as pd
import azure.functions as func
import time
import io
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
class Office():
'''
Uses the Office365 API and accesses the the Growth team sharepoint to extract the content
of a specific file into a pandas dataframe. The extracted datetime is also added to the dataframe
for uses in the table.
For now this class only reads in 1 specific file.
For future: Make it so it can take any xlsx file in any sharepoint.
'''
def __init__(self, Secret):
self.status_code = 200
self.site_url = '<%sharepoint site%>'
username = '<%service principal GUID%>'
password = str(Secret)
#Note Points to specific file now. TODO: Update to take any fname from Json.
self.relative_url = '<%link to file%>'
app_principal = {
'client_id': username,
'client_secret': password,
}
try: # setting up connection
self.context_auth = AuthenticationContext(url = self.site_url)
self.context_auth.acquire_token_for_app(client_id = app_principal['client_id'], client_secret = app_principal['client_secret'])
self.ctx = ClientContext(self.site_url, self.context_auth)
self.status_code = 200
except:
self.status_code = 403
def getData(self):
'''
Reads the file content in a byte stream format.
Uses the openpyxl to reformat this to be readeble for pandas.
'''
if self.status_code != 200:
return None, self.status_code
response = File.open_binary(self.ctx, self.relative_url) # gets a byte steam.
self.status_code = response.status_code
if self.status_code != 200:
return pd.DataFrame({'A' : []}), self.status_code #if error returns empty df.
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0) #set file object to start
df = pd.read_excel(bytes_file_obj, engine = 'openpyxl')
return df, self.status_code
def main(req: func.HttpRequest) -> func.HttpResponse:
now = time.strftime("%Y-%m-%d-%H:%M:%S") # Get extract time.
logging.info('Python HTTP trigger function processed a request.')
ClientSecret = req.params.get('Sharepoint-ClientSecret')
if not ClientSecret:
try:
req_body = req.get_json()
except ValueError:
ClientSecret = '<None>'
pass
else:
ClientSecret = req_body.get('value')
if ClientSecret:
now = time.strftime("%Y-%m-%d-%H:%M:%S") # Get extract time.
Of365 = Office(Secret = ClientSecret)
df, statusCode = Of365.getData()
# Some error handeling.
if statusCode == 403:
return func.HttpResponse(f"Authentication with secret key failed.\nStatus Code {statusCode}.", status_code = statusCode)
elif statusCode == 404:
return func.HttpResponse(f"File not found.\nStatus Code {statusCode}.", status_code = statusCode)
elif statusCode != 200:
return func.HttpResponse(f"Sometihng Failed.\nStatus Code {statusCode}", status_code = statusCode)
else:
logging.info(
'Success in connecting to Sharepoint API and generating {0} object class'.format(Office.__class__.__name__))
Date_Extracted = [now]*df.shape[0] # Created vector with time.
df ['Date_Extracted'] = Date_Extracted
return func.HttpResponse(df.to_json(orient="table"), status_code = 200)
else:
return func.HttpResponse(
"Bad Request: did not get any data.",
status_code=400
)
RequestInit:“需要双工选项”错误
此错误可能是由于版本不兼容造成的。要解决此错误,请按照评论中所述将 Visual Studio Code 升级到最新版本。
我已经使用您的代码创建了一个 Python V1 Azure 函数,并且能够将该函数部署到 Azure 函数应用程序。
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
}
}
需求.txt:
azure-functions
pandas
office365
office365-REST-Python-Client
传送门: