Я пытаюсь использовать службу gmail, чтобы читать данные входящих сообщений и показывать их в своем чате. Я хочу пройти аутентификацию, используя мой аккаунт Gmail. Когда я тестирую чат-бота в эмуляторе бота, все в порядке, проблема начинается при развертывании в сети ... Я не могу пройти проверку подлинности в сети.
string jsonPath2 = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "client_secret_webapp.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,},
"xxxx@gmail.com",
CancellationToken.None,
new FileDataStore(this.GetType().ToString())
);
}
var gmailService = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = this.GetType().ToString()
});
log.Info("ApplicationName" + ApplicationName);
var emailListRequest = gmailService.Users.Messages.List("xxxxx@gmail.com");
emailListRequest.LabelIds = "INBOX";
emailListRequest.MaxResults = 1;
emailListRequest.Q = "is:unread";
emailListRequest.IncludeSpamTrash = false;
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("xxxxxx@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 != "")
{
if (emailInfoResponse.Payload.Parts == null && emailInfoResponse.Payload.Body != null)
body = DecodeBase64String(emailInfoResponse.Payload.Body.Data);
else
body = GetNestedBodyParts(emailInfoResponse.Payload.Parts, "");
}
}
await context.PostAsync("Email list for: Date " + date + " ::::::::::: From: " + from + " :::::::::::: Subject " + subject + " : ::::::::::::: Body : " + body + " Email.id eshte " + email.Id);
}
}
}
}
static String DecodeBase64String(string s)
{
var ts = s.Replace("-", "+");
ts = ts.Replace("_", "/");
var bc = Convert.FromBase64String(ts);
var tts = Encoding.UTF8.GetString(bc);
return tts;
}
static String GetNestedBodyParts(IList<MessagePart> part, string curr)
{
string str = curr;
if (part == null)
{
return str;
}
else
{
foreach (var parts in part)
{
if (parts.Parts == null)
{
if (parts.Body != null && parts.Body.Data != null)
{
var ts = DecodeBase64String(parts.Body.Data);
str += ts;
}
}
else
{
return GetNestedBodyParts(parts.Parts, str);
}
}
return str;
}
}
private static byte[] FromBase64ForUrlString(string base64ForUrlInput)
{
int padChars = (base64ForUrlInput.Length % 4) == 0 ? 0 : (4 - (base64ForUrlInput.Length % 4));
StringBuilder result = new StringBuilder(base64ForUrlInput, base64ForUrlInput.Length + padChars);
result.Append(String.Empty.PadRight(padChars, '='));
result.Replace('-', '+');
result.Replace('_', '/');
return Convert.FromBase64String(result.ToString());
}
client_id.json
{"installed":{"client_id":"xxxxxxx- yyyyyy.apps.googleusercontent.com","project_id":"reademailbot","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"yyyyyyyyyyy","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
Отредактировано:
Я пытаюсь добавить код ниже:
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new
ClientSecrets
{
ClientId = "xx-
xxxxxxxxx.apps.googleusercontent.com",
ClientSecret = "xxxxxxxxxxxxxxxxxxxxx-lLO"
},
new[] {
GmailService.Scope.GmailReadonly,
GmailService.Scope.MailGoogleCom,
GmailService.Scope.GmailMetadata
},
"user",
CancellationToken.None,
new
FileDataStore("Drive.Auth.Store")).Result;
var gmailService = new Google.Apis.Gmail.v1.GmailService(new
BaseClientService.Initializer()
{
HttpClientInitializer = credential,
});
Но когда я пробую в Интернете, у меня все еще есть проблема:
Ошибка: redirect_uri_mismatch
URI перенаправления в запросе http://127.0.0.1:52158/authorize/, не совпадает с авторизованным для клиента OAuth. Чтобы обновить авторизованные URI перенаправления, посетите: https://console.developers.google.com/apis/credentials/oauthclient/673194113721-45u2pgl6m2l74ecf13c51fmmihrsu3jh.apps.googleusercontent.com?project=673194113721
Редактировать 2:
string[] Scopes = { GmailService.Scope.GmailReadonly };
string ApplicationName = "IkanbiBot";
var secrets = new ClientSecrets
{
ClientId = ConfigurationSettings.AppSettings["GMailClientId"],
ClientSecret = ConfigurationSettings.AppSettings["GMailClientSecret"]
};
var token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse { RefreshToken = ConfigurationSettings.AppSettings["GmailRefreshToken"] };
var credential = new UserCredential(new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = secrets
}), "xxxx@gmail.com", token);
var gmailService = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = this.GetType().ToString()
});
log4net.ILog log2 = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
await context.PostAsync("Aplication name is ..." + ApplicationName);
await context.PostAsync("secret name is ..." + secrets.ClientId + "_____ and .... " + secrets.ClientSecret + " ++++ token is " + token.RefreshToken);
var emailListRequest = gmailService.Users.Messages.List("xxxxx@gmail.com");
emailListRequest.LabelIds = "INBOX";
emailListRequest.MaxResults = 1;
emailListRequest.Q = "is:unread";
emailListRequest.IncludeSpamTrash = false;
// Get our emails
// Get our emails
var emailListResponse = emailListRequest.Execute();
//get our emails
log2.Info(emailListResponse.Messages);
await context.PostAsync("emailListResponse is ..." + emailListResponse.Messages.Count);
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("botikanbi@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 != "")
{
if (emailInfoResponse.Payload.Parts == null && emailInfoResponse.Payload.Body != null)
body = DecodeBase64String(emailInfoResponse.Payload.Body.Data);
else
body = GetNestedBodyParts(emailInfoResponse.Payload.Parts, "");
}
}
await context.PostAsync("Email list for: Date " + date + " ::::::::::: From: " + from + " :::::::::::: Subject " + subject + " : ::::::::::::: Body : " + body + " Email.id eshte " + email.Id);
}
}
}
}
static String DecodeBase64String(string s)
{
var ts = s.Replace("-", "+");
ts = ts.Replace("_", "/");
var bc = Convert.FromBase64String(ts);
var tts = Encoding.UTF8.GetString(bc);
return tts;
}
static String GetNestedBodyParts(IList<MessagePart> part, string curr)
{
string str = curr;
if (part == null)
{
return str;
}
else
{
foreach (var parts in part)
{
if (parts.Parts == null)
{
if (parts.Body != null && parts.Body.Data != null)
{
var ts = DecodeBase64String(parts.Body.Data);
str += ts;
}
}
else
{
return GetNestedBodyParts(parts.Parts, str);
}
}
return str;
}
}
private static byte[] FromBase64ForUrlString(string base64ForUrlInput)
{
int padChars = (base64ForUrlInput.Length % 4) == 0 ? 0 : (4 - (base64ForUrlInput.Length % 4));
StringBuilder result = new StringBuilder(base64ForUrlInput, base64ForUrlInput.Length + padChars);
result.Append(String.Empty.PadRight(padChars, '='));
result.Replace('-', '+');
result.Replace('_', '/');
return Convert.FromBase64String(result.ToString());
}
}
}
Теперь я использую этот код. Я получаю ответ от бота до:
"секретное имя ..." + secrets.ClientId + "_____ и ...." + secrets.ClientSecret + "++++ токен" "+ token.RefreshToken
Но остановитесь, когда попытаетесь прочитать электронную почту :(
Не могли бы вы помочь мне, пожалуйста, что происходит?