使用Powershell从GMAIL API获取消息正文

时间:2018-01-27 18:36:15

标签: powershell gmail gmail-api

我已成功设法使用Powershell连接到GMAIL API。 但是,我在找到获取邮件正文的位置时遇到了困难。

根据GMAIL的文件: https://developers.google.com/gmail/api/v1/reference/users/messages/get

应该在Payload属性下面: 获取https://www.googleapis.com/gmail/v1/users/userId/messages/id

这就是我的目的:

Invoke-WebRequest -Uri "https://www.googleapis.com/gmail/v1/users/me/messages/$($id)?access_token=$accesstoken&format=full" -Method Get | ConvertFrom-Json | select -ExpandProperty payload

以下是我得到的样本:

enter image description here

查看属性我似乎找不到任何远离邮件正文的内容。我错过了什么?

1 个答案:

答案 0 :(得分:0)

最终我找到了我想要的东西。 事实证明,谷歌在Base64中对身体进行了编码。这也是对象集层层次的。

$test = Invoke-WebRequest -Uri "https://www.googleapis.com/gmail/v1/users/me/messages/$($id)?access_token=$accesstoken&format=full" -Method Get | ConvertFrom-Json
$body = $test.payload.parts[0].parts.body.data[0] | Out-String
$d = $body.Replace(‘-‘,’+’).Replace(‘_’,’/’)
[String]$e = [System.Text.Encoding]::Ascii.GetString([System.Convert]::FromBase64String($d))
$e