I needed to upgrade google API OAuth 2 (but still use some old GData APIs) and used this post almost exclusively, which works great locally via iis express.
For convenience the code is:
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using Autofac;
using D.Core.Intelligence;
using Google.Apis.Auth.OAuth2;
using Google.GData.Analytics;
using Google.GData.Client;
namespace D.Analytics
{
public class AnalyticsModule : Module
{
private const string ServiceAccountEmail = "3504f5u6sqlm@developer.gserviceaccount.com";
private static readonly string KeyPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath ?? AppDomain.CurrentDomain.BaseDirectory, "Key.p12");
private static readonly X509Certificate2 Certificate = new X509Certificate2(KeyPath, "notasecret", X509KeyStorageFlags.Exportable);
private readonly ServiceAccountCredential _credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(ServiceAccountEmail)
{
Scopes = new[] { "https://www.googleapis.com/auth/analytics" }
}.FromCertificate(Certificate));
protected override void Load(ContainerBuilder builder)
{
builder.Register(c =>
{
_credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Wait();
var service = new AnalyticsService("D.com");
var requestFactory = new GDataRequestFactory("razbot2.0");
requestFactory.CustomHeaders.Add(string.Format("Authorization: Bearer {0}", _credential.Token.AccessToken));
service.RequestFactory = requestFactory;
return service;
});
builder.RegisterType<AnalyticsNewsletterTrackingSource>().As<INewsletterTrackingSource>();
builder.RegisterType<AnalyticsSearchTrackingSource>().As<ISearchTrackingSource>();
builder.RegisterType<AnalyticsEmailLinkTracker>().As<IEmailLinkTracker>();
}
}
}
Now this (seems) to work locally, but when I upload to my test server I get w3wp.exe exception, which I'm afraid I have no experience debugging.
I downloaded the source to the test server and tried to attach the process to the application with a debug point, only to get:
https://i.stack.imgur.com/SzhRy.png
I guess it all falls apart at:
Unhandled exception at 0x000007FD71459F99 (ntdll.dll) in w3wp.exe: 0xC0000374: A heap has been corrupted (parameters: 0x000007FD714AE610).
The entire symbol loading process is at: http://pastebin.com/Czc8e0cr
The inner exception is:
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx)
at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags)
at D.Analytics.AnalyticsModule..cctor()
With message:
The system cannot find the file specified.
Contrary to the message claim: The path to the file is certainly accurate as I've checked it myself via the debug point. The build action is set to 'content' and 'copy if newer' and I've even read in the values of the .p12 certificate into a bytearray and fed that to constructor itself, with no luck!
It could be the case that I'm doing something really silly in my code rather than something more deep but it just baffles me why it doesn't throw any exceptions locally!