2013-06-07 19 views
7

Ich möchte die Google Analytics API auf meiner MVC-Website verwenden, die Authentifizierung mit dem API-Dienstkonto und oauth2 mit keine Probleme auf meinem lokalen Host, aber sobald ich Azure bereitstellen, erhalte ich einen Fehler 502 :Google Analytics API auf Azure

"502 - Web server received an invalid response while acting as a gateway or proxy server. There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server."

heres mein Code:

const string ServiceAccountUser = "[email protected]count.com"; 
AssertionFlowClient client = new AssertionFlowClient(
     GoogleAuthenticationServer.Description, 
      new X509Certificate2(System.Web.Hosting.HostingEnvironment.MapPath("/Areas/Admin/xxxxxxxxxxxxxxxxxx-privatekey.p12"), 
       "notasecret", X509KeyStorageFlags.Exportable)) 
     { 
      Scope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue(), 
      ServiceAccountId = ServiceAccountUser //Bug, why does ServiceAccountUser have to be assigned to ServiceAccountId 
      //,ServiceAccountUser = ServiceAccountUser 
     }; 
     OAuth2Authenticator<AssertionFlowClient> authenticator = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState); 

ich kann nicht herausfinden was es verursacht? Fehle ich etwas in Azure?

Danke für jede Hilfe.

Antwort

9

Nach Stunden des Schmerzes auf genau dieses gleiche Problem, fand ich eine Arbeit um durch Aneinanderfügen verschiedener Quellen von Informationen.

Das Problem von dem Versuch entsteht, die p12-Datei aus der Azure Web-Site zu lesen, dh diese Zeile in meinem Code nicht

var key = new X509Certificate2(keyFile, keyPassword, X509KeyStorageFlags.Exportable); 

Keine Ahnung, warum, aber es funktioniert, wenn Sie die Datei in ein cer aufgeteilt und Datei key.xml?

Zuerst extrahieren Sie diese Dateien, (ich habe nur eine Konsole app)

// load pfx/p12 as "exportable" 
var p12Cert = new X509Certificate2(@"c:\Temp\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable); 

// export .cer from .pfx/.p12 
File.WriteAllBytes(@"C:\Temp\MyCert.cer", p12Cert.Export(X509ContentType.Cert)); 

// export private key XML 
string privateKeyXml = p12Cert.PrivateKey.ToXmlString(true); 

File.WriteAllText(@"C:\Temp\PrivateKey.xml", privateKeyXml); 

kopieren Sie sie dann auf Ihre Website sie dann wie so für mich Dies funktioniert

//Store the authentication description 
AuthorizationServerDescription desc = GoogleAuthenticationServer.Description; 

//Create a certificate object to use when authenticating 

var rsaCryptoServiceProvider = new RSACryptoServiceProvider(); 
rsaCryptoServiceProvider.FromXmlString(File.ReadAllText(keyFile)); 
var key = new X509Certificate2(certFile) {PrivateKey = rsaCryptoServiceProvider}; 


//Now, we will log in and authenticate, passing in the description 
//and key from above, then setting the accountId and scope 
var client = new AssertionFlowClient(desc, key) 
{ 
    //cliendId is your SERVICE ACCOUNT Email Address from Google APIs Console 
    //looks something like [email protected] 
    //~IMPORTANT~: this email address has to be added to your Google Analytics profile 
    // and given Read & Analyze permissions 
    ServiceAccountId = clientId, 
    Scope = "https://www.googleapis.com/auth/analytics.readonly" 
}; 

//Finally, complete the authentication process 
//NOTE: This is the first change from the update above 
var auth = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState); 

//First, create a new service object 
//NOTE: this is the second change from the update 
//above. Thanks to James for pointing this out 
var gas = new AnalyticsService(new BaseClientService.Initializer { Authenticator = auth }); 

in laden jetzt und Ich hoffe es hilft dir.

+1

Erstaunlich funktioniert perfekt! Vielen Dank, ich hätte das nie ohne deine Hilfe bekommen. –

+0

Goodone Martyn Hier in Google Analytics API-Implementierung haben wir nicht viel Hilfe und müssen schließlich Problem selbst beheben –

16

Ich lief auch in das gleiche Problem, aber X509KeyStorageFlags.MachineKeySet in den Konstruktor übergeben und behoben das Problem für mich.

X509Certificate2 certificate = new X509Certificate2(file, "key", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet); 
+0

Meine Güte. Es hat perfekt für mich funktioniert. Woher wussten Sie, das X509KeyStorageFlags.MachineKeySet-Flag zum Konstruktor hinzuzufügen? Warum funktioniert das? –

+0

Eigentlich funktionierte das nicht für mich. Ich bekomme weiterhin intermittierende 502 Fehler. –

+0

Das hat auch für mich perfekt funktioniert! Danke für das Teilen! – saurabhj