Guest User

Untitled

a guest
Dec 17th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. string jsonPath2 =
  2. System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath +
  3. "client_secret_p1.json";
  4.  
  5. UserCredential credential;
  6.  
  7.  
  8. string[] Scopes = { GmailService.Scope.GmailReadonly };
  9. string credPath = System.Environment.GetFolderPath(
  10. System.Environment.SpecialFolder.Personal);
  11.  
  12.  
  13.  
  14. using (var stream = new FileStream(jsonPath2, FileMode.Open,
  15. FileAccess.Read))
  16. {
  17. credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
  18. GoogleClientSecrets.Load(stream).Secrets,
  19. // This OAuth 2.0 access scope allows for read-only access
  20. to the authenticated
  21. // user's account, but not other types of account access.
  22. new[] { GmailService.Scope.GmailReadonly, GmailService.Scope.MailGoogleCom, GmailService.Scope.GmailModify, GmailService.Scope.GmailMetadata },
  23. CancellationToken.None,
  24. new FileDataStore(this.GetType().ToString())
  25.  
  26.  
  27. );
  28. }
  29. var gmailService = new GmailService(new BaseClientService.Initializer()
  30. {
  31. HttpClientInitializer = credential,
  32. ApplicationName = this.GetType().ToString()
  33. });
  34.  
  35. var emailListRequest = gmailService.Users.Messages.List("[email protected]");
  36. emailListRequest.LabelIds = "INBOX";
  37. emailListRequest.MaxResults = 1;
  38. emailListRequest.Q = "is:unread";
  39. emailListRequest.IncludeSpamTrash = false;
  40.  
  41. //emailListRequest.Q = "is:unread"; // This was added because I only wanted unread emails...
  42.  
  43. // Get our emails
  44. var emailListResponse = emailListRequest.Execute();
  45.  
  46. //get our emails
  47. log.Info(" emailListResponse is " + emailListResponse);
  48.  
  49. if (emailListResponse != null && emailListResponse.Messages != null)
  50. {
  51. //loop through each email and get what fields you want...
  52. foreach (var email in emailListResponse.Messages)
  53. {
  54.  
  55. var emailInfoRequest = gmailService.Users.Messages.Get("[email protected]", email.Id);
  56.  
  57. var emailInfoResponse = emailInfoRequest.Execute();
  58.  
  59.  
  60.  
  61. if (emailInfoResponse != null)
  62. {
  63. String from = "";
  64. String date = "";
  65. String subject = "";
  66. String body = "";
  67. //loop through the headers to get from,date,subject, body
  68. foreach (var mParts in emailInfoResponse.Payload.Headers)
  69. {
  70.  
  71.  
  72. if (mParts.Name == "Date")
  73. {
  74. date = mParts.Value;
  75. }
  76. else if (mParts.Name == "From")
  77. {
  78. from = mParts.Value;
  79. }
  80. else if (mParts.Name == "Subject")
  81. {
  82. subject = mParts.Value;
  83. }
  84.  
  85. if (date != "" && from != "")
  86. {
  87.  
  88. foreach (MessagePart p in emailInfoResponse.Payload.Parts)
  89. {
  90.  
  91.  
  92.  
  93.  
  94. if (emailInfoResponse.Payload.Parts == null && emailInfoResponse.Payload.Body != null)
  95. body = DecodeBase64String(emailInfoResponse.Payload.Parts[0].Body.Data);
  96. else
  97. body = GetNestedBodyParts(emailInfoResponse.Payload.Parts, "");
  98. }
  99.  
  100.  
  101. // else if (p.MimeType == "text/html")
  102. //{
  103. // byte[] data = FromBase64ForUrlString(p.Body.Data);
  104. // string decodedString = Encoding.UTF8.GetString(data);
  105.  
  106. //}
  107.  
  108. }
  109.  
  110.  
  111. }
  112. ///////////
  113. var source = body;
  114.  
  115. var parser = new HtmlParser();
  116. var dom = parser.Parse(source);
  117. var querySelector = dom.QuerySelector("a");
  118. //var textLink = querySelector.GetAttribute("href");
  119.  
  120.  
  121. await context.PostAsync("Info: Date );
  122.  
  123. {"installed":{"client_id":"com","project_id":"","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://www.googleapis.com/oauth2/v3/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
Add Comment
Please, Sign In to add comment