token是什么 Google Apps电子邮件设置API使用.Net和OAuth 2.0




refresh token作用 (2)

我想使用电子邮件设置API,但无法找到任何文档如何使用oAuth2身份验证使用此api

  1. 我使用正确的API吗?
  2. 我使用最新的API吗? (Google.GData.Apps.dll版本2.2.0)
  3. 我如何使用这个DLl与谷歌控制台项目与p12文件和serviceaccount?

根据Google文档,这是最新的api voor电子邮件设置: https//developers.google.com/admin-sdk/email-settings/

我在这个页面上找不到任何文档,如何在Oauth的.Net中使用它,但是在这个例子中,我看到了这个:

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

GoogleMailSettingsService service = new GoogleMailSettingsService("yourdomain", "your-apps");
service.setUserCredentials("adminUsername", "adminPassword");    
service.CreateSendAs("liz", "Sales", "[email protected]", "", "true");

所以寻找这些参考资料,我找到了这个网页: https : //code.google.com/p/google-gdata/或nuget包:www.nuget.org/packages/Google.GData.Apps/最新版本是2.2 0.0

因为我们正在切换到新的API使用控制台项目Oauth2和服务帐户我的问题是,我可以使用DLL也使用相同的身份验证为最新的API

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

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("[email protected]")
    {
        Scopes = scopes,
        User = "[email protected]"
    }.FromCertificate(certificate));

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

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

对于电子邮件设置API和任何其他GData类是服务,您可以使用类似于以下内容:

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

// Name of my cli application
string applicationName = "Test-OAuth2";

// Installed (non-web) application
string redirectUri = "urn:ietf:wg:oauth:2.0:oob";

// Requesting access to Contacts API and Groups Provisioning API
string scopes = "https://apps-apis.google.com/a/feeds/emailsettings/2.0/";

// Stuff usually found on client_secrets.json
string clientId = CLIENT_ID;
string clientSecret = CLIENT_SECRET;
string domain = CLIENT_DOMAIN;

// Prepare OAuth parameters
OAuth2Parameters parameters = new OAuth2Parameters() {
    ClientId = clientId,
    ClientSecret = clientSecret,
    RedirectUri = redirectUri,
    Scope = scopes
};

// Request authorization from the user
string url = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
Console.WriteLine("Authorize URI: " + url);

// Fetch the access code from console
parameters.AccessCode = Console.ReadLine();

// Get an access token
OAuthUtil.GetAccessToken(parameters);

try {
    // Create a new request factory so it uses our OAuth credentials
    GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory("apps", applicationName, parameters);

    GoogleMailSettingsService service = new GoogleMailSettingsService(domain, applicationName);
    service.RequestFactory = requestFactory;

    // Update the signature for the user testUserName
    service.UpdateSignature("[email protected]", "My tiny signature");
} catch (AppsException a) {
    Console.WriteLine("A Google Apps error occurred.");
    Console.WriteLine();
    Console.WriteLine("Error code: {0}", a.ErrorCode);
    Console.WriteLine("Invalid input: {0}", a.InvalidInput);
    Console.WriteLine("Reason: {0}", a.Reason);
}

您可以在这里查看随GData提供的OAuth示例中的完整示例: https : //code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/oauth2_sample/oauth2demo.cs

对于使用请求的其他GData类,流是相似的,但是您可以创建一个RequestSettings对象,并将其传递到您的GData请求对象构造函数中,如下所示: https : //code.google.com/p/google-gdata/source/浏览/中继/客户/ CS /样本/ oauth2_sample / oauth2demo.cs#63


我已经用示例从目录API https://developers.google.com/admin-sdk/directory/v1/quickstart/dotnet创建OAuth流,并使用它连接到emailsettings api服务。

    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", "[email protected]", "", "true");
        Console.Read();

    }






google-email-settings-api