使用.NET和OAuth的2.0谷歌企业应用套件电子邮件设置API套件、电子邮件、企业、OAuth

2023-09-04 23:06:33 作者:别碰我的女人

我要使用电子邮件设置API,但找不到任何文档如何使用此API与oAuth2认证

在我使用的是正确的API? 在我使用了最新的API? (Google.GData.Apps.dll版本2.2.0) 如何使用此DLL与谷歌控制台项目与P12文件 和serviceaccount?

按照谷歌文档,这是最新的API VOOR电子邮件设置:的https:/ /developers.google.com/admin-sdk/email-settings/

我找不到这个页面如何使用它在.net中使用OAuth的任何文件,但在这个例子中我看到这样的:

 使用Google.GData.Apps;
使用Google.GData.Apps.GoogleMailSettings;

GoogleMailSettingsService服务=新GoogleMailSettingsService(YOURDOMAIN,你 - 应用程序);
service.setUserCredentials(adminUsername,ADMINPASSWORD);
service.CreateSendAs(利兹,销售,sales@example.com,,真);
 

因此​​搜索这些引用我发现这个页面: 的https://$c$c.google.com/p/google-gdata/ 或的NuGet包:www.nuget.org/packages/Google.GData.Apps/ 最新的版本是2.2.0

由于我们切换到新的API的使用控制台项目Oauth2和服务帐户我的问题是,我可以使用DLL也采用同样的认证为最新的API

新的API的使用此身份验证方法:

  X509Certificate2证书=新X509Certificate2(@\位置\ P12File.p12,notasecret,X509KeyStorageFlags.Exportable);
IEnumerable的<字符串>范围=新的[] {DirectoryService.Scope.AdminDirectoryUser,DirectoryService.Scope.AdminDirectoryUserSecurity};
ServiceAccountCredential凭证=新ServiceAccountCredential(
    新ServiceAccountCredential.Initializer(ServiceAccountEmail@domain.com)
    {
        作用域=范围,
        用户=AdminAccount@domain.com
    } .FromCertificate(证书));

//创建服务。
VAR的服务=新DirectoryService中(
    新BaseClientService.Initializer()
    {
        HttpClientInitializer =凭证,
        应用程序名称=管理目录配置样本,
    });

service.Users.Delete(USERKEY).Execute();
 

解决方案

我已经把这个例子来创建目录的API的OAuth流 https://developers.google.com/admin-sdk/directory/v1 /快速启动/ DOTNET ,并用它来连接到emailsettings API服务。

 静态字符串[]作用域= {https://apps-apis.google.com/a/feeds/emailsettings/2.0/};
        静态字符串的ApplicationName =你 - 应用程序;

    静态无效的主要(字串[] args)
    {
        UserCredential凭证;


        使用(VAR流=
            新的FileStream(../../ client_secret.json,FileMode.Open,FileAccess.Read))
        {
            字符串credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath.credentials);

            凭据= GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(流).Secrets,
                范围,
                用户,
                CancellationToken.None,
                新FileDataStore(credPath,真))结果。
            Console.WriteLine(凭证文件保存到:+ credPath);
        }

        OAuth2Parameters参数=新OAuth2Parameters()
        {
            AccessToken = credential.Token.AccessToken
        };


        GOAuth2RequestFactory requestFactory =新GOAuth2RequestFactory(应用程序,应用程序名称,参数);
        GoogleMailSettingsService服务=新GoogleMailSettingsService(example.com,应用程序名称);
        service.RequestFactory = requestFactory;



        service.CreateSendAs(利兹,销售,sales@example.com,,真);
        Console.Read();

    }
 
谷歌邮件下载 谷歌邮件app下载 谷歌邮件手机版下载

I want to use the email settings API, but can not find any documentation how to use this api with the oAuth2 authentication

Am I using the correct API? Am I using the latest API? (Google.GData.Apps.dll Version 2.2.0) How can I use this DLl with an google console project with p12 file and serviceaccount?

According to the Google documentation this is the latest api voor email settings: https://developers.google.com/admin-sdk/email-settings/

I can not find any documentation on this page how to use it in .Net with Oauth, but in the example I see this:

using Google.GData.Apps;
using Google.GData.Apps.GoogleMailSettings;

GoogleMailSettingsService service = new GoogleMailSettingsService("yourdomain", "your-apps");
service.setUserCredentials("adminUsername", "adminPassword");    
service.CreateSendAs("liz", "Sales", "sales@example.com", "", "true");

So searching for these references I find this page: https://code.google.com/p/google-gdata/ or the nuget package: www.nuget.org/packages/Google.GData.Apps/ the latest version is 2.2.0

Since we are switching to the new api's using console projects Oauth2 and service-account my question is, can I use the dll also using the same authentication as for the newest api's

the new api's use this authentication method:

X509Certificate2 certificate = new X509Certificate2(@"\location\P12File.p12", "notasecret", X509KeyStorageFlags.Exportable);
IEnumerable<string> scopes = new[] { DirectoryService.Scope.AdminDirectoryUser, DirectoryService.Scope.AdminDirectoryUserSecurity };
ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer("ServiceAccountEmail@domain.com")
    {
        Scopes = scopes,
        User = "AdminAccount@domain.com"
    }.FromCertificate(certificate));

// Create the service.
var service = new DirectoryService(
    new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = "Admin directory Provisioning Sample",
    });

service.Users.Delete(userKey).Execute();

解决方案

I've took the example to create the OAuth flow from the directory API https://developers.google.com/admin-sdk/directory/v1/quickstart/dotnet and used it to connect to the emailsettings api service.

    static string[] Scopes = { "https://apps-apis.google.com/a/feeds/emailsettings/2.0/" };
        static string ApplicationName = "your-apps";

    static void Main(string[] args)
    {
        UserCredential credential;


        using (var stream =
            new FileStream("../../client_secret.json", FileMode.Open, FileAccess.Read))
        {
            string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials");

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            Console.WriteLine("Credential file saved to: " + credPath);
        }

        OAuth2Parameters parameter = new OAuth2Parameters()
        {
            AccessToken = credential.Token.AccessToken
        };


        GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory("apps", ApplicationName, parameter);
        GoogleMailSettingsService service = new GoogleMailSettingsService("example.com", ApplicationName);
        service.RequestFactory = requestFactory;



        service.CreateSendAs("liz", "Sales", "sales@example.com", "", "true");
        Console.Read();

    }