微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 OAUTH 和 C# 对 Google.Cloud.Storage.V1 进行身份验证,但从 JSON 创建凭据时出错无法识别的凭据类型

如何解决使用 OAUTH 和 C# 对 Google.Cloud.Storage.V1 进行身份验证,但从 JSON 创建凭据时出错无法识别的凭据类型

我正在尝试使用 OAuth2.0 和 C# 以及来自 Google 的 JSON 文件简单地对 Google Cloud Storage API 进行身份验证。

这是我的 OAuth JSON 文件

{
    "client_id": "ClientIdHere","project_id": "ProjectName","auth_uri": "https://accounts.google.com/o/oauth2/auth","token_uri": "https://oauth2.googleapis.com/token","auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs","client_secret": "ClientSecretHere","redirect_uris": [ "https://localhost:44349//RedirectPage.aspx" ]
  }

这是我正在使用的代码,我从这个post

借来的
       protected void DoTheThingsAgain()
        {
            try
            {
                string bucketName = "Daves-Bucket";
                string sharedkeyFilePath = "C:\\inetpub\\wwwroot\\GoogleAPITest\\GoogleAPITest\\App_Data\\JSONFile.json";
                GoogleCredential credential = null;

                using (var jsonStream = new FileStream(sharedkeyFilePath,FileMode.Open,FileAccess.Read,FileShare.Read))
                {
                    credential = GoogleCredential.FromStream(jsonStream);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

一切正常,直到碰到这一行:

**credential = GoogleCredential.FromStream(jsonStream);**

然后它给了我错误从 JSON 创建凭证时出错。无法识别的凭据类型。

如何为 OAuth 指定凭据类型?

我已经看到了其他解决方案,但在使用 OAuth 时没有,只有使用 ServiceAccounts。

请提前为您提供任何帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。