title: can I read Gmail messages while running the PowerShell
body: <p>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: <a href="https://developers.google.com/gmail/api/v1/reference/users/messages/get" rel="nofollow noreferrer">https://developers.google.com/gmail/api/v1/reference/users/messages/get</a> It should be right here</p>
  <pre><code>$clientId = &quot;1062910180948-ojlurj5s0uf9p5cemr6qi5tbdktsosek.apps.googleusercontent.com&quot;
  $clientSecret = &quot;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&quot;
  $refreshToken = '1//04SJSOVmkkaHYCgYIARAAGAQSNwF-L9IrbKgYGxUahD5jIAknai5oSSbqES_HOBxgvi7_qknj4B8rse9GduvUbETfIh266co8YIk'
  $headers = @{ 
     &quot;Content-Type&quot; = &quot;application/json&quot; 
  } 
  $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 = @{ 
     &quot;Content-Type&quot; = &quot;application/json&quot; 
  }
  
  $params = @{
     'Uri'         = &quot;https://www.googleapis.com/gmail/v1/users/me/messages?access_token=$accesstoken&quot;
     '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 (&quot;https://gmail.googleapis.com/gmail/v1/users/bvignesh@dartinnovations.com/messages/180dfdde1eaca598?access_token=$accesstoken&quot;) -Method Get | ConvertFrom-Json;
  
  Invoke-WebRequest -Uri &quot;https://www.googleapis.com/gmail/v1/users/me/messages?access_token=$accesstoken&quot; -Method Get
  
  Invoke-WebRequest -Uri (&quot;https://www.googleapis.com/gmail/v1/users/me/messages/$a&quot; + &quot;?access_token=$accesstoken&quot;) -Method Get
  
  
  
  write-host  $messages1.snippet;
  
  cmd /c pause
  
  </code></pre>