使用Python使用Microsoft Graph API Response获取Office 365邮箱文件夹结构

时间:2018-12-24 09:37:35

标签: python-3.x azure python-requests microsoft-graph

我正在寻找使用python(request)代码的Microsoft Graph API Response来获取O365邮箱中的文件夹和子文件夹。 我希望所有文件夹都具有各自的路径,但是API调用仅支持仅获取第1级的子文件夹,例如有一个收件箱文件夹,并且我希望在收件箱下创建文件夹,这会在收件箱下为我提供文件夹,但是如果我希望子文件夹位于内部, Inbox的子文件夹,我想使用Recurtion来实现此目的,我正在尝试使用recurtion,tree和编码逻辑技术来找到这样的解决方案,但我无法获得。

我还在这里附上示例代码,请帮助我。

import adal
import requests
import uuid
import json

class Example:
    staticstring = ''
    staticVariable = []
    staticArray = []
    staticFolder = []
    static_count = 0

tenant = "tenant_id"
client_secret = "client_secret_id"
client_id = 'client_id'

username = "user_name"
password = "user_pass"

authority = "https://login.microsoftonline.com/" + tenant
RESOURCE = "https://graph.microsoft.com"


context = adal.AuthenticationContext(authority)

# Use this for Client Credentials
token = context.acquire_token_with_client_credentials(RESOURCE, 
                                  client_id, client_secret)


# Use this for Resource Owner Password Credentials (ROPC)
#token = context.acquire_token_with_username_password(RESOURCE, 
                                username, password, client_id);
graph_api_endpoint = 'https://graph.microsoft.com/v1.0{0}'

request_url = 
graph_api_endpoint.format('/users/user_name/mailFolders/?$top=10')
headers = {
           'User-Agent' : 'python_tutorial/1.0',
           'Authorization' : 'Bearer 
                           {0}'.format(token["accessToken"]),
           'Accept' : 'application/json',
           'Content-Type' : 'application/json'
           }

response = requests.get(url = request_url, headers = headers)

mail_folders = response.json()

instance = Example()

def recuse(folders):
    instance.static_count += 1
    request_url = 
    graph_api_endpoint.format('/users/user_name/mailFolders/%s/ 
                 childFolders?$count=true' %(folders['id']))
    response = requests.get(url = request_url, headers = headers)
    response_data = response.json()
    top = response_data['@odata.count']
    if top != 0:
        path_list = []
        request_url = 
        graph_api_endpoint.format('/users/user_name/mailFolders/
                  %s/childFolders?$top=%d' %(folders['id'], top))
        response = requests.get(url = request_url, headers = 
                                                     headers)
        child_data = response.json()
        if len(child_data['value']) == 0:
            return path_list
        if folders['childFolderCount'] >= 2 :
            if folders['displayName'] in instance.staticFolder:
                print "folder already there"
            else:
                instance.staticFolder.append(
                              folders['displayName'])
        for index, each in enumerate(child_data['value']):
            #print "Each folder:- ", each['displayName']
            instance.staticArray.append(each['displayName'])
            recuse(each)
        instance.staticVariable = []
        for fo in instance.staticArray:
            if fo in instance.staticFolder:
                instance.staticArray.remove(fo)
            folder_parent = '/' + '/'.join(instance.staticFolder)
            folder_path = '/' + '/'.join(instance.staticArray)
        folder_parent += folder_path
        instance.staticVariable.append(folder_parent)
        print "instance.staticVariable:- ", folder_parent
        instance.staticArray = []
        return instance.staticVariable

all_folders = []
for folders in mail_folders['value']:
    folder_path = []
    if folders['childFolderCount'] == 0:
        instance.staticVariable.append(folders['displayName'])
    else:
        folder_path = []
        recurse_info = recuse(folders)
        print "Recurse info:- ", recurse_info
        all_folders.append(folder_path)
        if len(folder_path) > 0:
            for folder in folder_path:
                print folder
        print "FInal Folder structure:- ", instance.staticVariable

0 个答案:

没有答案