0

I am doing the PowerShell script in that when I am running the PowerShell script I can read the message in the PowerShell I can't get a clear idea from that please help anyone see this one. 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

$clientId = "1062910180948-ojlurj5s0uf9p5cemr6qi5tbdktsosek.apps.googleusercontent.com"
$clientSecret = "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"
$refreshToken = '1//04SJSOVmkkaHYCgYIARAAGAQSNwF-L9IrbKgYGxUahD5jIAknai5oSSbqES_HOBxgvi7_qknj4B8rse9GduvUbETfIh266co8YIk'
$headers = @{ 
   "Content-Type" = "application/json" 
} 
$body = @{
   client_id     = $clientId
   client_secret = $clientSecret
   refresh_token = $refreshToken
   grant_type    = 'refresh_token'
}
$params = @{
   'Uri'         = 'https://accounts.google.com/o/oauth2/token'
   'ContentType' = 'application/x-www-form-urlencoded'
   'Method'      = 'POST'
   'Headers'     = $headers
   'Body'        = $body
}
$accessTokenResponse = Invoke-RestMethod @params
$accesstoken = $($accessTokenResponse.access_token)
write-host $accesstoken

$headers = @{ 
   "Content-Type" = "application/json" 
}

$params = @{
   'Uri'         = "https://www.googleapis.com/gmail/v1/users/me/messages?access_token=$accesstoken"
   'ContentType' = 'application/json'
   'Method'      = 'GET'
   'Headers'     = $headers
   'Body'        = $body
}

$getMessagesResponse = Invoke-RestMethod @params
$messages = $($getMessagesResponse.messages)

#write-host $messages

$messageID = ($messages| Out-String);

write-host $messageID
#Seperates string on first message ID, places messageID into $result.

$result = $messageID.id[0];

write-host $result
#Acquires most recent message, using ID stored in result and access token.
$messages1 = Invoke-WebRequest -Uri ("https://gmail.googleapis.com/gmail/v1/users/bvignesh@dartinnovations.com/messages/180dfdde1eaca598?access_token=$accesstoken") -Method Get | ConvertFrom-Json;

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

Invoke-WebRequest -Uri ("https://www.googleapis.com/gmail/v1/users/me/messages/$a" + "?access_token=$accesstoken") -Method Get



write-host  $messages1.snippet;

cmd /c pause

1 Answers1

0

You retrieve the snippet field: write-host $messages1.snippet;
Instead, you need to get the payload:

enter image description here

The payload is of type MessagePart:

enter image description here

The body of which in turn is of type MessagePartBody:

enter image description here

Now the data of each MessagePartBody is a Base64 encoded string, that you need to decode to get the actual (human readable) text.
To decode base64 take a look at: How to decode a Base64 string?