2013-06-07 36 views
7

Tôi muốn sử dụng api phân tích google trong trang web MVC của mình, im xác thực bằng tài khoản dịch vụ api và oauth2 không có vấn đề trên localhost của tôi nhưng ngay sau khi tôi triển khai Azure tôi gặp lỗi 502 :Api Google Analytics trên 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 mã của tôi:

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

tôi không thể tìm hiểu Hãy làm cho nó? Am im thiếu cái gì đó trong Azure?

Cảm ơn bạn đã được trợ giúp.

Trả lời

9

Sau nhiều giờ đau đớn về cùng một vấn đề chính xác này, tôi đã tìm thấy một công việc xung quanh bằng cách ghép nối các nguồn thông tin khác nhau.

Vấn đề phát sinh từ cố gắng để đọc các tập tin p12 từ trang web Azure, tức là dòng này trong mã của tôi không

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

Không biết tại sao, nhưng nó hoạt động nếu bạn chia file thành một cer và tệp key.xml?

Thứ nhất, giải nén những tập tin này, (tôi chỉ sử dụng một ứng dụng console)

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

Sau đó sao chép chúng vào trang web của bạn sau đó tải chúng trong như vậy

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

này hiện đang làm việc cho tôi và Tôi hy vọng nó sẽ giúp bạn.

+1

Tuyệt vời làm việc hoàn hảo! Cảm ơn rất nhiều, tôi sẽ không bao giờ có mà không có sự giúp đỡ của bạn. –

+0

Goodone Martyn Ở đây trong triển khai google analytics api, chúng tôi không có nhiều trợ giúp và cuối cùng phải giải quyết vấn đề chính mình –

16

Tôi cũng gặp sự cố tương tự nhưng chuyển X509KeyStorageFlags.MachineKeySet vào hàm tạo cũng như khắc phục sự cố cho tôi.

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

Lòng tốt của tôi. Nó làm việc hoàn hảo cho tôi. Làm cách nào bạn biết thêm cờ X509KeyStorageFlags.MachineKeySet vào hàm tạo? Tại sao điều này hoạt động? –

+0

Thực ra, điều này không hiệu quả với tôi. Tôi tiếp tục nhận được lỗi 502 liên tục. –

+0

Điều này cũng làm việc hoàn hảo cho tôi! Cám ơn vì đã chia sẻ! – saurabhj

Các vấn đề liên quan