JustAGuy
JustAGuy

Reputation: 5941

Getting message body from GMAIL API using Powershell

I have successfully managed to connect to GMAIL API using Powershell. However, I'm having difficulties finding where to get the message body.

According to GMAIL's Documentation: https://developers.google.com/gmail/api/v1/reference/users/messages/get

It should be right here under the Payload attribute: GET https://www.googleapis.com/gmail/v1/users/userId/messages/id

This is what I run:

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

Here's a sample of what I get back:

enter image description here

Looking through the attributes I dont seem to find anything remotely close to the message body. What am I missing?

Upvotes: 1

Views: 874

Answers (1)

JustAGuy
JustAGuy

Reputation: 5941

Eventually I found what I was looking for. As it turns out Google encodes the body in Base64. This was also under layers of layers of object collections.

$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

Upvotes: 0

Related Questions