Guest User

Untitled

a guest
Dec 17th, 2018
108
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. "stelazrpp@gmail.com",
  24. CancellationToken.None,
  25. new FileDataStore(this.GetType().ToString())
  26.  
  27.  
  28. );
  29. }
  30. var gmailService = new GmailService(new BaseClientService.Initializer()
  31. {
  32. HttpClientInitializer = credential,
  33. ApplicationName = this.GetType().ToString()
  34. });
  35.  
  36. var emailListRequest = gmailService.Users.Messages.List("xxxx@gmail.com");
  37. emailListRequest.LabelIds = "INBOX";
  38. emailListRequest.MaxResults = 1;
  39. emailListRequest.Q = "is:unread";
  40. emailListRequest.IncludeSpamTrash = false;
  41.  
  42. //emailListRequest.Q = "is:unread"; // This was added because I only wanted unread emails...
  43.  
  44. // Get our emails
  45. var emailListResponse = emailListRequest.Execute();
  46.  
  47. //get our emails
  48. log.Info(" emailListResponse is " + emailListResponse);
  49.  
  50. if (emailListResponse != null && emailListResponse.Messages != null)
  51. {
  52. //loop through each email and get what fields you want...
  53. foreach (var email in emailListResponse.Messages)
  54. {
  55.  
  56. var emailInfoRequest = gmailService.Users.Messages.Get("xxx@gmail.com", email.Id);
  57.  
  58. var emailInfoResponse = emailInfoRequest.Execute();
  59.  
  60.  
  61.  
  62. if (emailInfoResponse != null)
  63. {
  64. String from = "";
  65. String date = "";
  66. String subject = "";
  67. String body = "";
  68. //loop through the headers to get from,date,subject, body
  69. foreach (var mParts in emailInfoResponse.Payload.Headers)
  70. {
  71.  
  72.  
  73. if (mParts.Name == "Date")
  74. {
  75. date = mParts.Value;
  76. }
  77. else if (mParts.Name == "From")
  78. {
  79. from = mParts.Value;
  80. }
  81. else if (mParts.Name == "Subject")
  82. {
  83. subject = mParts.Value;
  84. }
  85.  
  86. if (date != "" && from != "")
  87. {
  88.  
  89. foreach (MessagePart p in emailInfoResponse.Payload.Parts)
  90. {
  91.  
  92.  
  93.  
  94.  
  95. if (emailInfoResponse.Payload.Parts == null && emailInfoResponse.Payload.Body != null)
  96. body = DecodeBase64String(emailInfoResponse.Payload.Parts[0].Body.Data);
  97. else
  98. body = GetNestedBodyParts(emailInfoResponse.Payload.Parts, "");
  99. }
  100.  
  101.  
  102. // else if (p.MimeType == "text/html")
  103. //{
  104. // byte[] data = FromBase64ForUrlString(p.Body.Data);
  105. // string decodedString = Encoding.UTF8.GetString(data);
  106.  
  107. //}
  108.  
  109. }
  110.  
  111.  
  112. }
  113. ///////////
  114. var source = body;
  115.  
  116. var parser = new HtmlParser();
  117. var dom = parser.Parse(source);
  118. var querySelector = dom.QuerySelector("a");
  119. //var textLink = querySelector.GetAttribute("href");
  120.  
  121.  
  122. await context.PostAsync("Info: Date );
  123.  
  124. {"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