I am attempting to write some code that fetches emails from Gmail using the Gmail API documented here. I have successfully managed to retrieve a list of emails, and to get details of individual emails.
What I would like to do now is to use a BatchRequest to get details of multiple emails in a single call, but when I try this I get a 401 error with the message:
Google.Apis.Requests.RequestError Login Required [401] Errors [ Message[Login Required] Location[Authorization - header] Reason[required] Domain[global] ]
In the GetMessageInfo
method in the class below, there are three calls to the API:
Messages.List
This successfully returns a list of messagesMessages.Get
This successfully returns details of a single message- Finally, I attempt the same
Messages.Get
as in Step 2, but this time using aBatchRequest
, and this fails with the above error.
I am using the same service object each time, and in the case of Steps 2 & 3, I am using the same request.
QUESTION: Why can I get message details with a single request but not as part of a batch request?
public class ProofOfConcept
{
public void GetMessageInfo()
{
GmailService service = GetService();
UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List("me");
request.MaxResults = 1;
ListMessagesResponse response = request.Execute();
Message message = response.Messages.First();
Message fullMessageBySingleRequest = PerformSingleRequest(service, message.Id);
Message fullMessageByBatchRequest = PerformBatchRequest(service, message.Id, out RequestError error);
}
private Message PerformSingleRequest(GmailService service, string messageId)
{
UsersResource.MessagesResource.GetRequest request = service.Users.Messages.Get("me", messageId);
Message message = request.Execute();
return message;
}
private Message PerformBatchRequest(GmailService service, string messageId, out RequestError err)
{
UsersResource.MessagesResource.GetRequest messageRequest = service.Users.Messages.Get("me", messageId);
var batchRequest = new BatchRequest(service);
Message message = null;
RequestError requestError = null;
batchRequest.Queue<Message>(
messageRequest,
(content, error, i, msg) =>
{
message = content;
requestError = error;
});
batchRequest.ExecuteAsync().Wait();
err = requestError;
return message;
}
private GmailService GetService()
{
UserCredential credential;
using (var stream = new FileStream(
@".\ClientSecret\client_secret.json",
FileMode.Open,
FileAccess.Read))
{
string credPath = Environment.GetFolderPath(
Environment.SpecialFolder.Personal);
credPath = Path.Combine(
credPath,
".credentials/gmail-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] {GmailService.Scope.GmailReadonly},
"user",
CancellationToken.None,
new FileDataStore(credPath, true))
.Result;
}
// Create Gmail API service.
var service = new GmailService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Foo"
});
return service;
}
}