使用PowerShell v2.0通过Google API删除Gmail电子邮件


$user = "example@gmail.com"
$pass= "examplepassword" 
$secpasswd = ConvertTo-SecureString $user -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($pass, $secpasswd)
Invoke-RestMethod  'https://www.googleapis.com/gmail/v1/users/me/messages/0' -Method Delete -Credentials $cred

所以,我的问题是双重的。

我最初尝试使用http删除请求的Google API使用Invoke-webrequest删除Gmail电子邮件。但是,这是不起作用的,因为Powershell 2.0 does not support Invoke-WebRequest

此后,我转向尝试在IMAP和POP3实验后尝试利用Invoke-testhod,这两者都需要外部依赖关系(将.dlls添加到我正在使用的机器中并不是最佳的)。

因此,如果有人可以向我展示通过PowerShell的Google API删除电子邮件的适当方法,我将不胜感激。我提供了一些示例代码,涉及我正在使用的内容。请原谅它可能包含的任何错误,因为我对PowerShell是相对较新的错误,并且我的经验仍然有限。

gmail API将需要OAuth2身份验证,除非这是GSUIT/域名Admin/Gmail帐户,在这种情况下,您可以使用服务帐户进行身份验证。无论哪种情况,您都不能使用登录和密码。

我的powershell知识非常有限,您是否考虑过通过邮件服务器IMAP和SMTP直接执行此操作,而不是使用API。不知道是否可以使用PowerShell

更新:

我能够使用Invoke-webrequest做到这一点,您仍然需要先获取访问令牌。

Invoke-WebRequest -Uri "https://www.googleapis.com/gmail/v1/users/me/messages/0?access_token=$accesstoken"-Method Get | ConvertFrom-Json

也可以工作的接缝

Invoke-RestMethod -Uri "https://www.googleapis.com/gmail/v1/users/me/messages/0?access_token=$accesstoken"-Method Get 

如果您有兴趣,请在github上添加oauth的代码:google oauth powershell

最新更新