1
votes

I was previously working with the Gmail Atom Feed to fetch emails for my PowerShell script, but since the previous API is no longer supported and a new Gmail API was release, I was wondering if I can possibly write an equivalent script for the new Gmail API.

My previous script

$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential ("gmailemail", "pass")
[xml]$xml= $webclient.DownloadString("https://mail.google.com/mail/feed/atom")

How can I write a new script for the new gmail api, I didn't see any powershell example but I was thinking may be I can make a REST call to the email using the get method. But I am not sure how to connect.

Is there any support of PowerShell using new Gmail API.

I have setup my client Id in for gmail api and I would like to use the get method for Users.message to fetch a specific email message.

https://developers.google.com/gmail/api/v1/reference/users/messages/get

Atempt # 1

 $data = Invoke-RestMethod -Method Get -Uri "https://www.googleapis.com/gmail/v1/users/myemail%40gmail.com/profile?key=client-id"

I tried and it gives me the error in powershell

Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.

In Browser it gives me error

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],

  "code": 401,
  "message": "Login Required"
 }
}

Attemp 2

$headers = @{}
$headers.Add("Authorization", "Basic myemail@gmail.com pass")
$data = Invoke-RestMethod -Method Get -Headers $headers -Uri "https://www.googleapis.com/gmail/v1/users/myemail%40gmail.com/profile?key=client_id"
echo $data

Error

 Invoke-RestMethod : The remote server returned an error: (403) Forbidden.
1
Error 401 means that you still need to pass "Authorization: Oauth {access token}" in your headers. The credentials on the request aren't correct (or are missing). From this related SO post, if $resp is some other type (string, psobject and most probably null in this case), it will return an error message.abielita
You can check this tutorial about Sending mail from Gmail API using PowerShell.abielita

1 Answers

0
votes
Function Get-SubjectLine
{
#Acquires access token.
$accessToken = {accessToken};

        #Acquires most recent message ID using access token.
        $messageIDjson = Invoke-WebRequest -Uri "https://www.googleapis.com/gmail/v1/users/me/messages?access_token=$accessToken" -Method Get | ConvertFrom-Json;
        #Converts JSON message and thread ids into string.
        $messageID = ($messageIDjson | Out-String);
        #Seperates string on first message ID, places messageID into $result.
        $start = $messageID.indexOf("=") + 1;
        $end = $messageID.indexOf(";", $start);
        $length = $end - $start;
        $result = $messageID.substring($start, $length);
        #Acquires most recent message, using ID stored in result and access token.
        $messages = Invoke-WebRequest -Uri ("https://www.googleapis.com/gmail/v1/users/me/messages/$result" + "?access_token=$accessToken") -Method Get | ConvertFrom-Json;

return $messages.snippet;
}

This function returns the most recent message header from a user's inbox. You can use typical XML/JSON structure and change snippet to whatever you want. It has identical functionality to getting the atom feed headers. Make sure to acquire a refresh token and an access token before using this though, as it cannot access the Google API functions unless it has an access token that was acquired within the last hour or so, and access tokens must be refreshed at regular intervals.

If you want to learn more about refresh tokens and access tokens, I would suggest referring to this earlier answer, as Dalmto gave a particularly nice and detailed extension to her answer on GitHub.