Microsoft graph api 来自Microsoft Graph';s list_附件API,偶尔返回错误
API响应缓慢,有时返回既没有代码也没有说明的错误消息 为了给API的响应计时,我触发了它20次,然后记录了每次API调用的总时间和时间,以秒为单位 PFB用于测试列表附件API的Microsoft graph api 来自Microsoft Graph';s list_附件API,偶尔返回错误,microsoft-graph-api,Microsoft Graph Api,API响应缓慢,有时返回既没有代码也没有说明的错误消息 为了给API的响应计时,我触发了它20次,然后记录了每次API调用的总时间和时间,以秒为单位 PFB用于测试列表附件API的python脚本 # Initialize variables to track error and success error_count = 0 success_count = 0 # Allow token scope to not match requested scope. (Other auth libr
python
脚本
# Initialize variables to track error and success
error_count = 0
success_count = 0
# Allow token scope to not match requested scope. (Other auth libraries allow
# this, but Requests-OAuthlib raises an exception on scope mismatch by default.)
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
# Define the method to fetch attachments for a message
def fetch_attachment():
global error_count
global success_count
API_ROOT = 'https://graph.microsoft.com/'
API_VERSION = 'v1.0'
endpoint = API_ROOT + API_VERSION + RESOURCE
response = msgraph_session.get(endpoint).json()
if 'error' in response:
error_count += 1
LOG.info("Response from the attachments API is %r", response)
else:
success_count += 1
# Declare the statement that you want to time
STMT = '''
from __main__ import fetch_attachment
fetch_attachment()
'''
# Run the method and time it
if __name__ == '__main__':
time = timeit.timeit(STMT, number=20) # We will run the statement 20 times
LOG.info("Total time : %s", time)
LOG.info("Time per call: %s", float(time)/20)
LOG.info("Error Count %s", error_count)
LOG.info("Success Count %s", success_count)
输出
2018-08-02 11:36:53,510 - INFO - __main__ - Total time : 407.309007168
2018-08-02 11:36:53,510 - INFO - __main__ - Time per call: 20.3654503584
2018-08-02 11:36:53,511 - INFO - __main__ - Error Count 4
2018-08-02 11:36:53,511 - INFO - __main__ - Success Count 16
在20次中,有4次返回以下错误消息
{
"error":{
"innerError":{
"date":"2018-08-02T18:32:27",
"request-id":"a52a676c-20a6-46c8-a71f-24ce35b166d7"
},
"message":"",
"code":"UnknownError"
}
}
阿比
根据您的信息,我想您希望调用API/me/messages/{id}/attachments
。根据我的测试,当我在邮件中添加一个附件时,调用这个api并不慢。
但是当添加十个附件时,速度太慢了。因为它会响应每个附件的内容,所以如果每个文件都太大,则速度会很慢
我们可以找到这样的响应部分:
{
"@odata.type": "#microsoft.graph.fileAttachment",
"id": "{attachmentID}",
"lastModifiedDateTime": "2018-08-14T07:18:21Z",
"name": "{attachment name}",
"contentType": "{attachment type}",
"contentBytes": ""
}
我认为我们可以使用查询参数来定制响应。我们可以添加查询参数,使其响应不包含文件内容。我们可以通过如下API获得每个附件id
:
{
"@odata.type": "#microsoft.graph.fileAttachment",
"id": "{attachmentID}",
"lastModifiedDateTime": "2018-08-14T07:18:21Z",
"name": "{attachment name}",
"contentType": "{attachment type}",
"contentBytes": ""
}
https://graph.microsoft.com/beta/me/messages/{message id}/附件?$select=id、名称、大小
然后我们可以通过附件id获取附件的内容
https://graph.microsoft.com/beta/me/messages/{message id}/attachments/{attachment id}