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