问题
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();
回答1:
For the Email Settings API and any other GData classes that are Services you can use something like the following:
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("test@test.localhost.com", "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);
}
You can see a full example in the OAuth samples provided with GData here: https://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/oauth2_sample/oauth2demo.cs
For other GData classes which use Requests, the flow is similar but instead you create a RequestSettings object and pass it into your GData request object constructor as shown here: https://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/oauth2_sample/oauth2demo.cs#63
回答2:
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();
}
来源:https://stackoverflow.com/questions/27316394/google-apps-email-settings-api-using-net-and-oauth-2-0