2013-06-07 16 views
7

Chcę używać api google analytics w mojej witrynie MVC, im uwierzytelniam za pomocą konta usługi APi i oauth2, nie mam problemów na moim hoście lokalnym, ale zaraz po wdrożeniu na Azure pojawia się błąd 502 :Google Analytics Api na platformie 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."

herezje mój kod:

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); 

I cant dowiedzieć się, co jest przyczyną tego? Czy brakuje mi czegoś na Azure?

Dzięki za pomoc.

Odpowiedz

9

Po godzinach bólu na ten sam problem, znalazłem pracę poprzez połączenie różnych źródeł informacji.

Problem wynika z próby odczytu pliku p12 ze strony internetowej Azure, czyli tej linii w moim kodzie nie

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

pojęcia dlaczego, ale działa, jeśli podzielić plik do cer i plik key.xml?

Po pierwsze, należy wyodrębnić te pliki (tylko używane app konsoli)

// 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); 

skopiuj je na swojej stronie internetowej, a następnie załadować je w jak tak

//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 }); 

To teraz pracuje dla mnie i Mam nadzieję, że Ci to pomoże.

+1

Niesamowite działa idealnie! Wielkie dzięki, nigdy bym tego nie zrobił bez twojej pomocy. –

+0

Goodone Martyn W implementacji google analytics api nie mamy zbytniej pomocy i ostatecznie musimy rozwiązać problem samemu –

16

Wpadłem również na ten sam problem, ale przekazałem X509KeyStorageFlags.MachineKeySet do konstruktora i rozwiązałem problem.

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

Mój Boże. To zadziałało idealnie dla mnie. Skąd wiesz, aby dodać flagę X509KeyStorageFlags.MachineKeySet do konstruktora? Dlaczego to działa? –

+0

Właściwie to nie działało dla mnie. Nadal otrzymuję sporadyczne błędy 502. –

+0

To również zadziałało idealnie dla mnie! Dzięki za udostępnienie! – saurabhj

Powiązane problemy