2013-06-07 4 views
7

내 MVC 웹 사이트에서 Google Analytics api를 사용하고 싶습니다. api 서비스 계정을 사용하여 인증하고 oauth2는 내 localhost에 문제가 없지만 Azure에 배포하면 502 오류가 발생합니다. :Google Analytics Api on 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 내 코드 : 그것을 일으키는 원인이 뭐죠

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

알아낼 질수? Azure에서 뭔가 빠진거야?

도움 주셔서 감사합니다.

답변

9

이 똑같은 문제에 대한 몇 시간의 고통 후에, 나는 다양한 정보 소스를 결합하여 해결할 수있는 방법을 발견했습니다. 문제는 내 코드에서이 줄 즉, 푸른 웹 사이트에서 P12 파일을 읽으려고에서 발생

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

아니 생각이 왜 실패,하지만 당신이 CER로 파일을 분할하는 경우 작동 key.xml 파일? 첫째

,이 파일의 압축을 풉니 다, 다음 그래서

//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 }); 
이 지금 나를 위해 작동

및처럼 그들을에서로드

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

그런 다음 귀하의 웹 사이트에 복사 (난 그냥 콘솔 응용 프로그램을 사용) 나는 그것이 당신을 돕기를 바랍니다.

+1

정말 놀라운 작품! 고마워, 너의 도움 없이는 절대로 그렇게하지 못했을거야. –

+0

Goodone Martyn 여기 구글 분석 API를 구현에 우리는 많은 도움이 없어 결국 결국 스스로 문제를 해결할 수있다 –

16

또한 동일한 문제가 발생했지만 X509KeyStorageFlags.MachineKeySet을 생성자로 전달할 때도 문제가 해결되었습니다.

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

내 세상에. 그것은 나를 위해 완벽하게 작동했습니다. 생성자에 X509KeyStorageFlags.MachineKeySet 플래그를 추가하는 방법을 알고 계셨습니까? 왜이게 효과가 있니? –

+0

실제로, 이것은 나를 위해 작동하지 않았다. 간헐적 인 502 오류가 계속 발생합니다. –

+0

이것은 나를 위해 완벽하게 작동했습니다! 공유 해줘서 고마워! – saurabhj