I set up the Gmail API credentialing process as follows:
string clientSecret = Settings.ClientSecret;
string clientId = Settings.ClientId;
Task<UserCredential> tCredential;
ClientSecrets clientSecrets = new ClientSecrets {ClientId = clientId, ClientSecret = clientSecret};
tCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
clientSecrets,
Scopes,
"user",
CancellationToken.None);
UserCredential credential;
try
{
credential = tCredential.Result;
}
catch (Exception ex)
{
throw;
}
_service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Draft Sender", //applicationName,
});
I didn't set the credential to save in any kind of file. However, when I run the code, I only get prompted to allow access to the email account once. After the initial authorization, subsequent runs do not generate a prompt to authorize. This was actually a good thing until I tried to change the email address I was accessing.
The way it works is I have two email addresses: a personal email address and a test email address. I had the program working so that it accessed the content of my personal email address. Then, I needed to develop code that deleted the messages, so I decided to switch to a test email address.
In order to access the email addresses using the Gmail API, you have to enable the API and generate a 'client id' and 'client secret' in the Google developer's console for each email address you want to access. I had generated both values for both the personal email and the test email.
Next, I updated the client id and client secret in the code so that my test email was targeted rather than my personal email.
When I updated the clientId and clientSecret to the new email address, I initially got an authorization prompt, but the authorization prompt was asking for access to the old email account. I went ahead and authorized just to see what would happen. Now, even with the changed clientId and secret, the program accesses the original email address. I have even tried changing the secret for the original email address, but the program still has access.
I have to believe there is some kind of file that is storing the credentials, because how else could the program access the email inbox?? How do I find this file or get this code to access a different email inbox? Do I need to manually create a file in order to bypass whatever is automatically being done?
Ok, I was able to work around the problem without actually fixing it. By refreshing the secret from the Gmail API console for all email addresses involved, I forced the authentication to expire.
Then, when I went in to reauthorize the program (when I ran it in Visual Studio, and IE came up with the authorization prompt), I made sure to change the gmail account (listed in the upper right) to the email account I wanted to authorize.
This worked; the fact that it worked and didn't work the first time suggests to me that there are some authentication issues...but I don't know what they are and speculation is inherently, well, speculative since I would have to know exactly what I did.
If there is someone who understands what is going on, I would really like to know, because I am still having problems changing the state of my authenticator (the scope) after granting access to an account (in other words: once I grant access, it seems that the access is locked in at that level, and no amount of changing options from the code seems to do anything).