使用没有文件的服务帐户凭据初始化 firebase 管理 - System.NotSupportedException - “不支持标记‘27’。”
Posted
技术标签:
【中文标题】使用没有文件的服务帐户凭据初始化 firebase 管理 - System.NotSupportedException - “不支持标记‘27’。”【英文标题】:Initialize firebase management with Service Account Credential without files- System.NotSupportedException - "Tag '27' not supported." 【发布时间】:2022-01-02 15:27:14 【问题描述】: public static class ServiceAccountManager
public static ServiceAccountKey Datakey get; private set;
public static void CreateKey(string serviceAccountEmail)
Datakey = Service.Projects.ServiceAccounts.Keys.Create(
new CreateServiceAccountKeyRequest(),
"projects/-/serviceAccounts/" + serviceAccountEmail)
.Execute();
Console.WriteLine("Created key: " + Datakey.Name);
.......
用私钥初始化
private static void InitializeFirebaseManagement()
var servicecredential = new ServiceAccountCredential(new
ServiceAccountCredential.Initializer(CloudManager.ProjectId)
.FromPrivateKey("-----BEGIN PRIVATE KEY-----
"+ServiceAccountManager.Datakey.PrivateKeyData+"-----END PRIVATE KEY-----")
);
var credential =GoogleCredential.FromServiceAccountCredential(servicecredential);
if (CloudManager.Credential.IsCreateScopedRequired)
credential =CloudManager.Credential.CreateScoped(FirebaseManagementService.Scope.CloudPlatform);
_firebaseManagementService = new FirebaseManagementService(
new BaseClientService.Initializer()
HttpClientInitializer = credential,
ApplicationName = CloudManager.ApplicationName
);
问题
无法从给定的 PKCS8 私钥中提取密钥(方法 FromPrivateKey())
输出
Value ServiceAccountManager.Datakey.PrivateKeyData =ewogICJ0eXBlIjogInNlcnZpY2VfYWNjb3VudCIsCiAgInByb2plY3RfaWQiOiAiZ2FtYW5ldC0yOSIsCiAgInByaXZhdGVfa2V5X2lkIjogImEyOGNjNzA4ZWFhNjFlZTBmNGQ0NWQ2NTRjZOXd2ZEM1VS9XNGlST3FSQXJmVFk3TzhcbjFQZEdhWVU3UHhJTjlsemhXTFJTR0xyWVp3MmtjeS95OG1TYVFUQUx1K3lzcjJVWVJGVjNJd1k1MmM1anVIQzBcbmNNTUxFSX ................... GhfcHJvdmlkZXJfeDUwOV9jZXJ0X3VybCI6ICJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9vYXV0aDIvdjEvY2VydHMiLAogICJjbGllbnRfeDUwOV9jZXJ0X3VybCI6ICJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9yb2JvdC92MS9tZXRhZGF0YS94NTA5L2dhbWFuZXRjb20lNDBnYW1hbmV0LTI5LmlhbS5nc2VydmljZWFjY291bnQuY29tIgp9Cg == P>
异常
System.NotSupportedException:“不支持标记 '27'。”
在 Google.Apis.Auth.OAuth2.Pkcs8.Asn1.Decoder.Decode() 在 Google.Apis.Auth.OAuth2.Pkcs8.Asn1.Decode(Byte[] bs) 在 Google.Apis.Auth.OAuth2.Pkcs8.DecodeRsaParameters(String pkcs8PrivateKey) 在 Google.Apis.Auth.OAuth2.ServiceAccountCredential.Initializer.FromPrivateKey(字符串 私钥) 在 CloudResourceManagers.Program.InitializeFirebaseManagement() 在 /Users/mac/Desktop/TestApp/CloudResourceManager/Program.cs:第 84 行 在 CloudResourceManagers.Program.Main(String[] args) 在 /Users/mac/Desktop/TestApp/CloudResourceManager/Program.cs:55
【问题讨论】:
选择标签时请注意,不要只写一个字母然后回车。那么你肯定会选择错误的标签,比如 C 语言标签而不是 C#。 【参考方案1】:Java 代码:
InputStream stream = new ByteArrayInputStream(key.decodePrivateKeyData());
GoogleCredential credential = GoogleCredential.fromStream(stream);
C#:
var buffer = Convert.FromBase64String(ServiceAccountManager.DataKey.PrivateKeyData);
Stream stream = new MemoryStream(buffer);
GoogleCredential credential = GoogleCredential.FromStream(stream);
【讨论】:
以上是关于使用没有文件的服务帐户凭据初始化 firebase 管理 - System.NotSupportedException - “不支持标记‘27’。”的主要内容,如果未能解决你的问题,请参考以下文章
使用服务帐户凭据模式的 Firebase 分发 android 应用程序的权限被拒绝
以编程方式为 BigQuery 服务传递服务帐户凭据(JSON 文件)