使用Microsoft Graph API搜索、删除和移动电子邮件的Python包

graphish的Python项目详细描述


graphish

使用Microsoft Graph API从Office 365邮箱中搜索和删除邮件的Python包

当前功能

* Searching
    * Create new Search
    * Update a Search
    * Get Search Folder
    * Get Search messages
    * Delete Search
* Deleting
    * Delete a Message
* Mailbox Rules
    * List Mailbox Rules
* Users
    * Return a list of email addresses in your Azure AD Tenant
* MailFolder
    * Create MailFolder
    * Move message to a MailFolder
    * List messages in a MailFolder

安装

若要使用,请在本地安装软件包:

安装

本地:

bash git clone git@github.com:swimlane/graphish.git cd graphish pip install setup.py

OS X和Linux:

pip install graphish

窗口:

pip install graphish

用法

若要使用,您需要在azure ad中创建一个新应用程序。请按照以下说明获取相应的机密:

https://docs.microsoft.com/en-us/graph/auth-register-app-v2

也请查看这篇关于Graphish的博客文章https://swimlane.com/blog/swimlane open-sources graphish/

此外,如果您希望graphish搜索您的azure租户中的所有用户,则需要在注册期间为您的azure广告应用程序提供user.read.all权限。

获得此信息后,您可以执行以下操作:

图形连接器

要使用graphish必须首先创建包含所有身份验证信息的graphconnector对象。一旦您创建了此连接器对象,则用户将此对象作为必需参数提供给此包中的其他类。

以下是生成graphconnector对象的不同方法,取决于您为应用程序选择的身份验证工作流方法。

委派的身份验证

若要将graphish与委派权限和用户名和密码一起使用,您需要提供clientid、clientsecret、tenantid以及您的帐户用户名和密码。

通过使用委派的身份验证(单页、Web应用程序、移动和本机应用程序-授予身份验证流),您可以通过不传递userprincipalname来搜索自己的邮箱,或者如果要搜索另一个邮箱,请提供userprincipalname(电子邮件地址):

使用(单页、Web应用程序、移动和本机应用程序)身份验证流为您的帐户创建连接器:

fromgraphishimportGraphConnectorconnector=GraphConnector(clientId='14b8e5asd-c5a2-4ee7-af26-53461f121eed',# you applications clientIdclientSecret='OdhG1hXb*UB/ho]A?0ZCci13KMflsHDy',# your applications clientSecrettenantId='c1141d00-072f-1eb9-2526-12802571dd41',# your applications Azure Tenant IDuserPrincipalName='first.last@myorg.onmicrosoft.com',# the user's mailbox you want to searchpassword='somepassword'# password of your normal or admin account)

使用(单页、Web应用程序、移动和本机应用程序)身份验证流为其他用户邮箱创建连接器:

fromgraphishimportGraphConnector# For legacy app grant flow provide a username and passwordconnector=GraphConnector(clientId='14b8e5asd-c5a2-4ee7-af26-53461f121eed',# you applications clientIdclientSecret='OdhG1hXb*UB/ho]A?0ZCci13KMflsHDy',# your applications clientSecrettenantId='c1141d00-072f-1eb9-2526-12802571dd41',# your applications Azure Tenant IDuserPrincipalName='first.last@myorg.onmicrosoft.com',# the user's mailbox you want to searchpassword='somepassword'# password of your normal or admin account)

应用程序验证

要将graphish用于应用程序权限,您需要提供clientid、clientsecret和tenantid。

通过使用应用程序身份验证(客户端凭据授予身份验证流),您可以搜索特定邮箱或所有邮箱。

使用服务/后台程序身份验证流为您的帐户创建连接器:

fromgraphishimportGraphConnector# For backend / client_credential auth flow just supply the followingconnector=GraphConnector(clientId='14b8e5asd-c5a2-4ee7-af26-53461f121eed',# you applications clientIdclientSecret='OdhG1hXb*UB/ho]A?0ZCci13KMflsHDy',# your applications clientSecrettenantId='c1141d00-072f-1eb9-2526-12802571dd41',# your applications Azure Tenant ID)

使用服务/后台程序身份验证流为其他用户邮箱创建连接器:

fromgraphishimportGraphConnector# For backend / client_credential auth flow just supply the followingconnector=GraphConnector(clientId='14b8e5asd-c5a2-4ee7-af26-53461f121eed',# you applications clientIdclientSecret='OdhG1hXb*UB/ho]A?0ZCci13KMflsHDy',# your applications clientSecrettenantId='c1141d00-072f-1eb9-2526-12802571dd41'# your applications Azure Tenant ID)

创建新搜索

一旦确定了适当的身份验证并创建了graphconnector对象,则可以创建新的search对象。拥有搜索对象之后,就可以创建新搜索、从搜索中检索邮件、获取搜索文件夹、更新搜索文件夹或删除搜索。当您创建新的搜索时,这将在用户邮箱中创建一个隐藏的文件夹(用户无法看到),它将基于您的搜索筛选器query进行填充。

创建(或实例化)一个search对象时,可以指定搜索的范围。有三个与指定搜索相关的用例:

  • search类的user principal name参数提供用户主体名称
  • 为th上的userprincipalname参数提供"me"e使用用户名和密码验证工作流时搜索
  • 默认值:不为search类上的userprincipalname参数提供值。这将通过listusers端点拉入你的azure广告中的所有用户。

注意:如果使用应用程序身份验证工作流,则可以传入一个或一个userprincipalname列表。如果不传入userprincipalname,则搜索将尝试搜索azure ad租户中的所有邮箱!

fromgraphishimportSearchsearch=Search(connector)new_search=search.create(searchFolderName='Phishing Search',sourceFolder='inbox',filterQuery="contains(subject, 'EXPIRES')")

从搜索中获取消息

您可以使用搜索对象的同一实例并使用消息方法来检索在搜索过程中标识的消息:

# get all the messages in your search folderformessageinsearch.messages():print(message)# Returns all attributes from a messageprint(message['id'])# returns the message IDprint(message['headers'])# Returns the RFC822 headers of the message

获取邮件文件夹列表

如果您需要邮箱中的邮件文件夹列表,可以使用文件夹方法检索它们:

* Searching
    * Create new Search
    * Update a Search
    * Get Search Folder
    * Get Search messages
    * Delete Search
* Deleting
    * Delete a Message
* Mailbox Rules
    * List Mailbox Rules
* Users
    * Return a list of email addresses in your Azure AD Tenant
* MailFolder
    * Create MailFolder
    * Move message to a MailFolder
    * List messages in a MailFolder
0

获取用户列表

如果您需要搜索范围内所有用户的列表:

* Searching
    * Create new Search
    * Update a Search
    * Get Search Folder
    * Get Search messages
    * Delete Search
* Deleting
    * Delete a Message
* Mailbox Rules
    * List Mailbox Rules
* Users
    * Return a list of email addresses in your Azure AD Tenant
* MailFolder
    * Create MailFolder
    * Move message to a MailFolder
    * List messages in a MailFolder
1

将邮件移动到文件夹

如果您已经执行了搜索并希望将邮件移动到邮件文件夹,可以执行以下操作:

* Searching
    * Create new Search
    * Update a Search
    * Get Search Folder
    * Get Search messages
    * Delete Search
* Deleting
    * Delete a Message
* Mailbox Rules
    * List Mailbox Rules
* Users
    * Return a list of email addresses in your Azure AD Tenant
* MailFolder
    * Create MailFolder
    * Move message to a MailFolder
    * List messages in a MailFolder
2

创建新的邮件夹

您还可以使用mailfolder类创建新的mailfolder

* Searching
    * Create new Search
    * Update a Search
    * Get Search Folder
    * Get Search messages
    * Delete Search
* Deleting
    * Delete a Message
* Mailbox Rules
    * List Mailbox Rules
* Users
    * Return a list of email addresses in your Azure AD Tenant
* MailFolder
    * Create MailFolder
    * Move message to a MailFolder
    * List messages in a MailFolder
3

更新搜索

如果要更改执行的搜索,可以更新搜索文件夹和各个条件,如搜索文件夹的名称、源文件夹(要搜索的根目录)或筛选器查询本身:

* Searching
    * Create new Search
    * Update a Search
    * Get Search Folder
    * Get Search messages
    * Delete Search
* Deleting
    * Delete a Message
* Mailbox Rules
    * List Mailbox Rules
* Users
    * Return a list of email addresses in your Azure AD Tenant
* MailFolder
    * Create MailFolder
    * Move message to a MailFolder
    * List messages in a MailFolder
4

删除搜索

您还可以使用delete方法删除执行的搜索:

* Searching
    * Create new Search
    * Update a Search
    * Get Search Folder
    * Get Search messages
    * Delete Search
* Deleting
    * Delete a Message
* Mailbox Rules
    * List Mailbox Rules
* Users
    * Return a list of email addresses in your Azure AD Tenant
* MailFolder
    * Create MailFolder
    * Move message to a MailFolder
    * List messages in a MailFolder
5

列出邮箱规则

此外,您还可以列出任何邮箱规则:

* Searching
    * Create new Search
    * Update a Search
    * Get Search Folder
    * Get Search messages
    * Delete Search
* Deleting
    * Delete a Message
* Mailbox Rules
    * List Mailbox Rules
* Users
    * Return a list of email addresses in your Azure AD Tenant
* MailFolder
    * Create MailFolder
    * Move message to a MailFolder
    * List messages in a MailFolder
6

其他示例

您可以在这里找到其他示例

发布历史记录

  • 葡萄向pypi的初始释放
  • 添加了获取所有用户和将邮件移动到指定邮件文件夹的功能
  • 乔什·里卡德–@msadministratorrickardja@live.com

    根据麻省理工学院的许可证发行。有关详细信息,请参见许可证。

    贡献

    1. 分叉(https://github.com/swimlane/graphish/fork" rel="nofollow">https://github.com/swimlane/graphish/fork)
    2. 创建功能分支(git checkout-b feature/foobar
    3. 提交更改(git commit-am'add some foobar'
    4. 推到分支(git push origin feature/foobar
    5. 创建新的拉取请求

    欢迎加入QQ群-->: 979659372 Python中文网_新手群

    推荐PyPI第三方库


    热门话题
    ArrayList Java中的搜索字符串   另一个web应用程序的java访问会话   另一个应用程序中的活动和服务之间的java通信   java根据Json字符串类型将Json字符串转换为对象   eclipse如何解决java中的错误异常。lang.NoSuchMethodError:'java。字符串javax。摆动JOptionPane。showInputDialog(java.lang.String)'   线程“main”java中的安卓异常。lang.NoClassDefFoundError:org/codehaus/jackson/JsonParseException   java如何在安卓 emulator上显示Mat图像?使用NDK   Java在本地读取测试源文件,但在服务器上读取失败   java dowhile循环用于计算输入数字中的数字。故障排除代码   JAva初学者在编写获取成本的方法时遇到困难   java是shell游戏。我如何让物体移动,特别是在特定的曲线上,但顺序是随机的?   java如何区分两个同名的JButton   java为什么我在Spring Boot中需要一个接口?   java将文件路径插入数据库将删除\   使用InterfaceType初始化java对象   java如何部署一个分为Angular、Spring Boot和MySQL的项目?   java如何使用Symja解决不等式?