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.
"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