我真的潜水一个让我睡不着觉的问题。
基本上,我们有一个应用程序,可以为公司的每个用户在GMail(' domaincompany.com')上获取HTML电子邮件签名,并在新的HTML签名上设置更新的个人数据。
这是如何实施的:
public static string getGMailFirma(string mail) {
GoogleMailSettingsService service = null;
string[] domainUserGMail;
string result;
try {
/* Recuperamos los datos del usuario de GMail: *
* - Pos 0: usuario *
* - Pos 1: dominio */
domainUserGMail = Util.getUserDomainGMail(mail);
/* Iniciamos el servicio de GApps */
service = new GoogleMailSettingsService(domainUserGMail[1], "company.FirmaCorporativa");
service.setUserCredentials(PropertiesManager.getUserGApps(), PropertiesManager.getPassGApps());
result = service.RetrieveSignature(domainUserGMail[0]).getPropertyValueByName("signature").ToString();
return result;
} catch (Exception ex) {
throw ex;
}
}
所以,几周前这个不再起作用,显示错误信息:“执行身份验证请求后返回意外结果:404”执行后:
result = service.RetrieveSignature(domainUserGMail [0])。getPropertyValueByName(" signature")。ToString();
搜索网络文档和不同的资源,我们意识到它应该实现OAuth2,所以我们尝试了这个代码修改:
public static string getGMailFirma(string mail) {
GoogleMailSettingsService service = null;
string[] domainUserGMail;
string result;
try {
const string ServiceAccountEmail = "xxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com";
var certificate = new X509Certificate2("Key_admin.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(ServiceAccountEmail) {
Scopes = new[] { "https://apps-apis.google.com/a/feeds/emailsettings/2.0/" }
}.FromCertificate(certificate));
if (!credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)
throw new InvalidOperationException("Access token request failed.");
var requestFactory = new GDataRequestFactory(null);
requestFactory.CustomHeaders.Add("Authorization: Bearer " + credential.Token.AccessToken);
/* Recuperamos los datos del usuario de GMail: *
* - Pos 0: usuario *
* - Pos 1: dominio */
domainUserGMail = Util.getUserDomainGMail(mail);
/* Iniciamos el servicio de GApps */
service = new GoogleMailSettingsService(domainUserGMail[1], "CompanyFirmaCorporativa");
service.RequestFactory = requestFactory;
service.setUserCredentials(PropertiesManager.getUserGApps(), PropertiesManager.getPassGApps());
result = service.RetrieveSignature(domainUserGMail[0]).getPropertyValueByName("signature").ToString();
return result;
} catch (Exception ex) {
throw ex;
}
}
但在同一行执行后仍然会出现另一个错误403(禁止)。
对于上次测试,我之前已完成以下步骤:
拜托,如果有人能帮我,如果我做错了,我将非常感激。
提前致谢。
干杯!
答案 0 :(得分:1)
今天我疯了要与同样的问题,但对于“RetrieveSendAs”里在相同的API范围,只是这一分钟解决它!
'初始化程序'需要一个'用户'参数,其中包含网站管理员的主电子邮件:
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(Globals.GaServiceEmail)
{
User = "gapps-admin-login@example.org",
Scopes = new[] { "https://apps-apis.google.com/a/feeds/emailsettings/2.0/" }
}.FromCertificate(gaCertificate));
您还需要从代码中删除旧的“service.setUserCredentials”行。