I created an intent to access newest emails comes in inbox and it was working ok.
After i change my password of my gmail account i cannot access my gmail using bot. Previously i was able to access email in my chatbot without authorization, and was working very good. I try to create new OAuth 2.0 client IDs json file but still doesn't work.
Here is the code :
string jsonPath2 =
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath +
"client_secret_p1.json";
UserCredential credential;
string[] Scopes = { GmailService.Scope.GmailReadonly };
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
using (var stream = new FileStream(jsonPath2, FileMode.Open,
FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows for read-only access
to the authenticated
// user's account, but not other types of account access.
new[] { GmailService.Scope.GmailReadonly, GmailService.Scope.MailGoogleCom, GmailService.Scope.GmailModify, GmailService.Scope.GmailMetadata },
"xxxx@gmail.com",
CancellationToken.None,
new FileDataStore(this.GetType().ToString())
);
}
var gmailService = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = this.GetType().ToString()
});
var emailListRequest = gmailService.Users.Messages.List("xxxx@gmail.com");
emailListRequest.LabelIds = "INBOX";
emailListRequest.MaxResults = 1;
emailListRequest.Q = "is:unread";
emailListRequest.IncludeSpamTrash = false;
//emailListRequest.Q = "is:unread"; // This was added because I only wanted unread emails...
// Get our emails
var emailListResponse = emailListRequest.Execute();
//get our emails
log.Info(" emailListResponse is " + emailListResponse);
if (emailListResponse != null && emailListResponse.Messages != null)
{
//loop through each email and get what fields you want...
foreach (var email in emailListResponse.Messages)
{
var emailInfoRequest = gmailService.Users.Messages.Get("xxx@gmail.com", email.Id);
var emailInfoResponse = emailInfoRequest.Execute();
if (emailInfoResponse != null)
{
String from = "";
String date = "";
String subject = "";
String body = "";
//loop through the headers to get from,date,subject, body
foreach (var mParts in emailInfoResponse.Payload.Headers)
{
if (mParts.Name == "Date")
{
date = mParts.Value;
}
else if (mParts.Name == "From")
{
from = mParts.Value;
}
else if (mParts.Name == "Subject")
{
subject = mParts.Value;
}
if (date != "" && from != "")
{
foreach (MessagePart p in emailInfoResponse.Payload.Parts)
{
if (emailInfoResponse.Payload.Parts == null && emailInfoResponse.Payload.Body != null)
body = DecodeBase64String(emailInfoResponse.Payload.Parts[0].Body.Data);
else
body = GetNestedBodyParts(emailInfoResponse.Payload.Parts, "");
}
//else
if (p.MimeType == "text/html")
//{
//byte[] data = FromBase64ForUrlString(p.Body.Data);
// string decodedString = Encoding.UTF8.GetString(data);
//}
}
}
///////////
var source = body;
var parser = new HtmlParser();
var dom = parser.Parse(source);
var querySelector = dom.QuerySelector("a");
//var textLink = querySelector.GetAttribute("href");
await context.PostAsync("Info: Date );
client secret format:
{"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"]}}
I revoke access to this app. I try to read data from gmail, the bot ask me to login , i put the credential of my account nad i receive this windows :
中获取img文本之后,我无法访问数据:(