2016-07-13 26 views
1

Google Cloud Platformサービスアカウントとキーファイルを使用して.NETでGoogleストレージにoauth-2接続を確立するにはどうすればよいですか?使用可能なサンプルとドキュメントではサービスアカウントについて扱われておらず、APIが混乱しており、頻繁に変更されるようであるため、既存のドキュメントは疑わしいものになります。重要なAPIの名前空間とバージョンを含む、実用的なコード例が最適です。Google Cloud Platformサービスアカウントとキーファイルを使用した.NETのGoogleストレージ

+0

あなたはDotNetOpenAuth /を見ましたか? https://github.com/DotNetOpenAuth/DotNetOpenAuth/wiki/Security-Scenarios – weismat

+0

@weismat見てみましょう。今のところ私はコア・タスクを別の方法で解決しています。 Googleは.NET開発を容易にするインセンティブはありません。 –

答えて

1

Cloud Storage JSON API documentationのコードサンプルでは、​​GOOGLE_APPLICATION_CREDENTIALSをダウンロードしたJSONキーを指すように設定した後、残念なことにApplication Default認証情報の使用についてのみ説明しています。

しかしあなたはクラウドストレージのドキュメントに与えられたCreateStorageClient()メソッドを適応させるために使用できるエクスポートP12キー、とのサービスアカウントを使用するためのdocumentation for the Google API Client Library for .NETのサンプルがあります:

public StorageService CreateStorageClient() 
{ 
    String serviceAccountEmail = "SERVICE_ACCOUNT_EMAIL_HERE"; 
    var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable); 

    ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(serviceAccountEmail) 
    { 
     Scopes = new[] { StorageService.Scope.DevstorageFullControl } 
    }.FromCertificate(certificate)); 

    var serviceInitializer = new BaseClientService.Initializer() 
    { 
    ApplicationName = "Storage Sample", 
    HttpClientInitializer = credential 
    }; 

    return new StorageService(serviceInitializer); 
} 

同じ私は現在設定された.NETのdevの環境を持っていないが、それは一般的なアイデアを与える必要がありますように私はこれらをテストしていません

public StorageService CreateStorageClient() 
{ 
    GoogleCredential credential; 
    using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) 
    { 
    credential = GoogleCredential.FromStream(stream) 
     .CreateScoped(StorageService.Scope.DevstorageFullControl); 
    } 

    var serviceInitializer = new BaseClientService.Initializer() 
    { 
    ApplicationName = "Storage Sample", 
    HttpClientInitializer = credential 
    }; 

    return new StorageService(serviceInitializer); 
} 

注:API docsに基づいて、直接JSONのキーを使用する方法使い方。 Cloud Storageドキュメントの更新をリクエストして、JSONキーを使用する例を追加します。

+0

私たちは根本的な問題を回避する方法を開発して以来、この解決方法を検証するまでには時間がかかるかもしれません。 –

関連する問題