|
| 1 | +// Copyright 2018, Google Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using FirebaseAdmin; |
| 17 | +using FirebaseAdmin.Auth; |
| 18 | +using Google.Apis.Auth.OAuth2; |
| 19 | + |
| 20 | +namespace FirebaseAdmin.Snippets |
| 21 | +{ |
| 22 | + class FirebaseAppSnippets |
| 23 | + { |
| 24 | + static void InitSdkWithServiceAccount() |
| 25 | + { |
| 26 | + // [START initialize_sdk_with_service_account] |
| 27 | + FirebaseApp.Create(new AppOptions() |
| 28 | + { |
| 29 | + Credential = GoogleCredential.FromFile("path/to/serviceAccountKey.json"), |
| 30 | + }); |
| 31 | + // [END initialize_sdk_with_service_account] |
| 32 | + } |
| 33 | + |
| 34 | + static void InitSdkWithApplicationDefault() |
| 35 | + { |
| 36 | + // [START initialize_sdk_with_application_default] |
| 37 | + FirebaseApp.Create(new AppOptions() |
| 38 | + { |
| 39 | + Credential = GoogleCredential.GetApplicationDefault(), |
| 40 | + }); |
| 41 | + // [END initialize_sdk_with_application_default] |
| 42 | + } |
| 43 | + |
| 44 | + static void InitSdkWithRefreshToken() |
| 45 | + { |
| 46 | + // [START initialize_sdk_with_refresh_token] |
| 47 | + FirebaseApp.Create(new AppOptions() |
| 48 | + { |
| 49 | + Credential = GoogleCredential.FromFile("path/to/refreshToken.json"), |
| 50 | + }); |
| 51 | + // [END initialize_sdk_with_refresh_token] |
| 52 | + } |
| 53 | + |
| 54 | + static void InitSdkWithDefaultConfig() |
| 55 | + { |
| 56 | + // [START initialize_sdk_with_default_config] |
| 57 | + FirebaseApp.Create(); |
| 58 | + // [END initialize_sdk_with_default_config] |
| 59 | + } |
| 60 | + |
| 61 | + static void InitDefaultApp() |
| 62 | + { |
| 63 | + // [START access_services_default] |
| 64 | + // Initialize the default app |
| 65 | + var defaultApp = FirebaseApp.Create(new AppOptions() |
| 66 | + { |
| 67 | + Credential = GoogleCredential.GetApplicationDefault(), |
| 68 | + }); |
| 69 | + Console.WriteLine(defaultApp.Name); // "[DEFAULT]" |
| 70 | + |
| 71 | + // Retrieve services by passing the defaultApp variable... |
| 72 | + var defaultAuth = FirebaseAuth.GetAuth(defaultApp); |
| 73 | + |
| 74 | + // ... or use the equivalent shorthand notation |
| 75 | + defaultAuth = FirebaseAuth.DefaultInstance; |
| 76 | + // [END access_services_default] |
| 77 | + } |
| 78 | + |
| 79 | + static void InitCustomApp() |
| 80 | + { |
| 81 | + var defaultOptions = new AppOptions() |
| 82 | + { |
| 83 | + Credential = GoogleCredential.GetApplicationDefault(), |
| 84 | + }; |
| 85 | + var otherAppConfig = new AppOptions() |
| 86 | + { |
| 87 | + Credential = GoogleCredential.GetApplicationDefault(), |
| 88 | + }; |
| 89 | + |
| 90 | + // [START access_services_nondefault] |
| 91 | + // Initialize the default app |
| 92 | + var defaultApp = FirebaseApp.Create(defaultOptions); |
| 93 | + |
| 94 | + // Initialize another app with a different config |
| 95 | + var otherApp = FirebaseApp.Create(otherAppConfig, "other"); |
| 96 | + |
| 97 | + Console.WriteLine(defaultApp.Name); // "[DEFAULT]" |
| 98 | + Console.WriteLine(otherApp.Name); // "other" |
| 99 | + |
| 100 | + // Use the shorthand notation to retrieve the default app's services |
| 101 | + var defaultAuth = FirebaseAuth.DefaultInstance; |
| 102 | + |
| 103 | + // Use the otherApp variable to retrieve the other app's services |
| 104 | + var otherAuth = FirebaseAuth.GetAuth(otherApp); |
| 105 | + // [END access_services_nondefault] |
| 106 | + } |
| 107 | + |
| 108 | + static void InitWithServiceAccountId() |
| 109 | + { |
| 110 | + // [START initialize_sdk_with_service_account_id] |
| 111 | + FirebaseApp.Create(new AppOptions() |
| 112 | + { |
| 113 | + Credential = GoogleCredential.GetApplicationDefault(), |
| 114 | + ServiceAccountId = "[email protected]", |
| 115 | + }); |
| 116 | + // [END initialize_sdk_with_service_account_id] |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments