title: New Gmail API : Support for powershell
body: <p>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.</p>
  
  <p>My previous script</p>
  
  <pre><code>$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")
  </code></pre>
  
  <p>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. </p>
  
  <p>Is there any support of PowerShell using new Gmail API.</p>
  
  <p>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.</p>
  
  <p><a href="https://developers.google.com/gmail/api/v1/reference/users/messages/get" rel="nofollow">https://developers.google.com/gmail/api/v1/reference/users/messages/get</a></p>
  
  <p><strong>Atempt # 1</strong></p>
  
  <pre><code> $data = Invoke-RestMethod -Method Get -Uri "https://www.googleapis.com/gmail/v1/users/myemail%40gmail.com/profile?key=client-id"
  </code></pre>
  
  <p>I tried and it gives me the error in powershell</p>
  
  <pre><code>Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
  </code></pre>
  
  <p>In Browser it gives me error</p>
  
  <pre><code>{
   "error": {
    "errors": [
     {
      "domain": "global",
      "reason": "required",
      "message": "Login Required",
      "locationType": "header",
      "location": "Authorization"
     }
    ],
  
    "code": 401,
    "message": "Login Required"
   }
  }
  </code></pre>
  
  <p><strong>Attemp 2</strong></p>
  
  <pre><code>$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
  </code></pre>
  
  <p>Error</p>
  
  <pre><code> Invoke-RestMethod : The remote server returned an error: (403) Forbidden.
  </code></pre>