public void CanConvert()
        {
            // Arrange
            const GmailScopes scopes        = GmailScopes.Readonly;
            string            readOnlyScope = GmailHelper.GetGmailScopesField("ReadOnlyScope");

            // Act
            string scopeString = scopes.ToScopeString();

            // Assert
            scopeString.ShouldBeEquivalentTo(readOnlyScope);
        }
        /// <summary>
        /// Convert <see cref="GmailScopes"/> to a by space separated string.
        /// </summary>
        /// <returns></returns>
        public static string ToScopeString(this GmailScopes gmailScopes)
        {
            GmailScopes[] scopes            = gmailScopes.GetFlagEnumValues();
            string[]      scopeValueStrings = new string[scopes.Length];
            for (int i = 0; i < scopes.Length; i++)
            {
                switch (scopes[i])
                {
                case GmailScopes.Readonly:
                    scopeValueStrings[i] = ReadOnlyScope;
                    break;

                case GmailScopes.Modify:
                    scopeValueStrings[i] = ModifyScope;
                    break;

                case GmailScopes.Compose:
                    scopeValueStrings[i] = ComposeScope;
                    break;

                case GmailScopes.Insert:
                    scopeValueStrings[i] = InsertScope;
                    break;

                case GmailScopes.Labels:
                    scopeValueStrings[i] = LabelsScope;
                    break;

                case GmailScopes.Send:
                    scopeValueStrings[i] = SendScope;
                    break;

                case GmailScopes.ManageBasicSettings:
                    scopeValueStrings[i] = ManageBasicSettingsScope;
                    break;

                case GmailScopes.ManageSensitiveSettings:
                    scopeValueStrings[i] = ManageSensitiveSettingsScope;
                    break;

                case GmailScopes.FullAccess:
                    scopeValueStrings[i] = FullAccessScope;
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(gmailScopes));
                }
            }

            return(string.Join(" ", scopeValueStrings));
        }
        public async Task Play()
        {
            // ----------------------
            // --- SETUP ---
            // ----------------------

            // Either use from config
            const GmailScopes        scopes            = GmailScopes.Readonly | GmailScopes.Compose;
            string                   privateKey        = SettingsManager.GetPrivateKey();
            string                   tokenUri          = SettingsManager.GetTokenUri();
            string                   clientEmail       = SettingsManager.GetClientEmail();
            string                   emailAddress      = SettingsManager.GetEmailAddress();
            ServiceAccountCredential accountCredential = new ServiceAccountCredential
            {
                PrivateKey  = privateKey,
                TokenUri    = tokenUri,
                ClientEmail = clientEmail
            };
            var client = new GmailClient(accountCredential, emailAddress, scopes);

            /*// Or use from downloaded JSON file directly
             * const string path = "C:\\Users\\Me\\Documents\\Gmail-Project.json";
             * var initializer = GmailClientInitializer.Initialize(path, scopes);
             * client = new GmailClient(initializer, emailAddress);*/

            // ----------------------
            // --- USAGE EXAMPLES ---
            // ----------------------
            // Send a plain text email
            Message sentMessage = await client.Messages.SendAsync(emailAddress, "The subject", "Plain text body");

            // Send a HTML email
            sentMessage = await client.Messages.SendAsync(emailAddress, "The subject", "<h1>HTML body</h1>", isBodyHtml : true);

            // Get the users profile
            Profile profile = await client.GetProfileAsync();

            // Get inbox messages
            IList <Message> messages = await client.Messages.ListAsync();

            // Get starred messages
            IList <Message> starredMessages = await client.Messages.ListByLabelAsync(Label.Starred);

            // List all labels
            IList <Label> labels = await client.Labels.ListAsync();

            // List all drafts
            IList <Draft> drafts = await client.Drafts.ListAsync();
        }
        public void WithMultipleScopes_ReturnsSpaceSeparatedString()
        {
            // Arrange
            const GmailScopes scopes = GmailScopes.Readonly | GmailScopes.Modify | GmailScopes.Labels | GmailScopes.Insert | GmailScopes.Send;
            // order shouldn't matter
            var scopeList = new List <string>
            {
                GmailHelper.GetGmailScopesField("LabelsScope"),
                GmailHelper.GetGmailScopesField("ReadOnlyScope"),
                GmailHelper.GetGmailScopesField("ModifyScope"),
                GmailHelper.GetGmailScopesField("SendScope"),
                GmailHelper.GetGmailScopesField("InsertScope"),
            };

            // Act
            string scopeString = scopes.ToScopeString();

            // Assert
            var parsedScopeList = scopeString.Split(' ').ToList();

            parsedScopeList.ShouldAllBeEquivalentTo(scopeList);
        }
Beispiel #5
0
 /// <summary>
 /// Access to all Gmail services.
 /// </summary>
 /// <param name="accountCredential">The Google Account Credentials</param>
 /// <param name="emailAddress">The emailaddress of the user to impersonate</param>
 /// <param name="scopes">The Gmail scopes required to access the users data</param>
 public GmailClient(ServiceAccountCredential accountCredential, string emailAddress, GmailScopes scopes)
     : this(accountCredential, emailAddress, scopes.ToScopeString())
 {
 }
 public static GmailClientInitializer Initialize(string path, GmailScopes scopes)
 {
     return(Initialize(path, scopes.ToScopeString()));
 }