Reputation: 139
I have a requirement to implement gmail into my web application. While implementing Gmail in my web application, I am getting the mentioned exception.
I have tried the steps mentioned in Gmail API Quickstart... https://developers.google.com/gmail/api/quickstart/dotnet
As I am trying to create an object of GmailService, I am getting an exception. My code is as follows...
static UserCredential GetGmailUserCredential()
{
UserCredential credential;
string[] scopes = { GmailService.Scope.GmailReadonly };
string credPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), ".credentials/gmail-dotnet-quickstart.json");
try
{
using (var stream = new System.IO.FileStream(@"D:\Projects_Temp\Learning\Webform\GSuite\client_secret.json", System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
Environment.UserName,
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
}
catch (Exception ex)
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientID, ClientSecret = clientSecret }
, scopes
, Environment.UserName
, CancellationToken.None
, new FileDataStore(credPath, true)).Result;
}
return credential;
}
public static GmailService GetGmailClient()
{
var credential = GetGmailUserCredential();
// Create Drive API service.
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});
return service;
}
public static List<Label> GetLabels(GmailService service)
{
// Define parameters of request.
UsersResource.LabelsResource.ListRequest request = service.Users.Labels.List("me");
// List labels.
return request.Execute().Labels.ToList();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (GmailService service = GSuiteHelper.GetGmailClient())
{
var lst = GSuiteHelper.GetLabels(service);
}
}
}
Upvotes: 1
Views: 3056
Reputation: 945
Check the Web.config of your web application. I saw a duplicate entry in mine. One had all caps public token. So I am guessing it is case sensitive and didn't over-write when I upgraded the version. So it kept using the old version number, which I had obviously uninstalled. Hope this helps. These was the duplicate that existed.
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Google.Apis.Auth" publicKeyToken="4B01FA6E34DB77AB" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.27.1.0" newVersion="1.27.1.0" />
</dependentAssembly>
Upvotes: 1