2013-06-07 13 views
7

私はMVC WebサイトでGoogle AnalyticsのAPIを使用したいと思います。apiサービスアカウントとoauth2を使用して認証していますが、ローカルホストで問題はありませんが、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

この全く同じ問題の痛みを抱えてから、私は様々な情報源をつなぎ合わせることで回避策を見つけました。問題は、私のコードでこの行すなわち、AzureのWebサイトから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ここGoogle AnalyticsのAPIの実装では、私たちは多くの助けがなく、最終的に問題を解決する必要があります –

16

私も同じ問題が発生しましたが、X509KeyStorageFlags.MachineKeySetをコンストラクタに渡しても問題は解決しました。

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

私の良さ。それは私のために完璧に働いた。コンストラクタにX509KeyStorageFlags.MachineKeySetフラグを追加する方法を知りましたか?なぜこれは機能しますか? –

+0

実際、これは私のためには機能しませんでした。断続的な502のエラーが引き続き発生します。 –

+0

これは私にとっても完璧に機能しました!共有ありがとう! – saurabhj

関連する問題