Reputation: 11
I am writing a C# desktop application, in which I want to read all emails of users (other then mine). I have already created a project on GMail console. When I am reading self email the application is working fine, but I am not able to read the email / messages of other user. The sample code available in GMail document read the label of owner email id but able to read label of other user (using GMail Email ID / Profile ID)
UserCredential credential;
using (var stream = new FileStream("CredentialFile.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
logMe("Token : " + credential.Token.AccessToken);
// Create Gmail API service.
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
UsersResource.LabelsResource.ListRequest request = service.Users.Labels.List("XXXXXXXXXXXXXXXXXXXXX");
// List labels.
IList<Label> labels = request.Execute().Labels;
Console.WriteLine("Labels:");
if (labels != null && labels.Count > 0)
{
foreach (var labelItem in labels)
{
Console.WriteLine("{0}", labelItem.Name);
}
}
else
{
Console.WriteLine("No labels found.");
}
Console.Read();
Upvotes: 1
Views: 602
Reputation: 116868
FileDataStore stores the credentials of the user you have loged in as in %appData% denoted with the name of the user which you have supplied as "user"
If you want to change the user you are currently logged in as you need to change "user" to something else. its just a string so it can be what ever you like to denote this user. Then have the user login with their account.
If you want to understand how FileDataStore works this might interest you Google .net – FileDatastore demystified
Upvotes: 1