提问者:小点点

我可以在运行PowerShell时阅读Gmail消息吗


我正在执行PowerShell脚本,因为当我运行PowerShell脚本时,我可以阅读PowerShell中的消息,我不能从中得到一个清晰的想法,请帮助任何人看到这个。我已经成功地使用Powershell连接到GMAILAPI。但是,我很难找到在哪里可以获得消息正文。根据GMAIL的文档:https://developers.google.com/gmail/api/v1/reference/users/messages/get它应该就在这里

$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个答案

匿名用户

您检索片段字段:write e-host$message ages1.片段;
相反,您需要获取有效负载

有效负载的类型为MessagePart:

其主体又是MessagePartBody类型:

现在每个MessagePartBody的数据都是一个Base64编码的字符串,您需要对其进行解码才能获得实际的(人类可读的)文本。
要解码base64,请查看:如何解码Base64字符串?