Google Oauth 错误:应设置至少一个客户端机密(已安装或 Web)

Posted

技术标签:

【中文标题】Google Oauth 错误:应设置至少一个客户端机密(已安装或 Web)【英文标题】:Google Oauth error: At least one client secrets (Installed or Web) should be set 【发布时间】:2015-05-03 18:04:54 【问题描述】:

我正在使用 Google 的 Oauth 2.0 通过我们的服务器将视频上传到 Youtube。 我的客户 ID 是“服务帐户”。我下载了 json 密钥并将其添加到我的解决方案中。

以下是相关代码:

 private async Task Run(string filePath)
        
            UserCredential credential;
            var keyUrl = System.Web.HttpContext.Current.Server.MapPath("~/content/oauth_key.json");
            using (var stream = new FileStream(keyUrl, FileMode.Open, FileAccess.Read))
            
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    // This OAuth 2.0 access scope allows an application to upload files to the
                    // authenticated user's YouTube channel, but doesn't allow other types of access.
                    new[]  YouTubeService.Scope.YoutubeUpload ,
                    "user",
                    CancellationToken.None
                );
            

            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            
                HttpClientInitializer = credential,
                ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
            );

当我运行它时,我收到此错误:应设置至少一个客户端机密(已安装或 Web)。

但是,在我的 json 中没有“客户端密码”:


  "private_key_id": "9d98c06b3e730070806dcf8227578efd0ba9989b",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIICdQIBADANBgkqhk etc,
  "client_email": "546239405652-8igo05a5m8cutggehk3rk3hspjfm3t04@developer.gserviceaccount.com",
  "client_id": "546239405652-8igo05a5m8cutggehk3rk3hspjfm3t04.apps.googleusercontent.com",
  "type": "service_account"

所以我假设我忽略了一些东西。 也许我不能使用“服务帐户”?不知道……

【问题讨论】:

你试过了吗?有什么解决办法吗? 【参考方案1】:

使用json文件的解决方案很相似。

这里是使用从json 文件和ServiceAccountCredential 创建的GoogleCredential 对象创建VisionService 的示例。

GoogleCredential credential;
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))

    credential = GoogleCredential.FromStream(stream)
        .CreateScoped(VisionService.Scope.CloudPlatform);


var service = new VisionService(new BaseClientService.Initializer()

    HttpClientInitializer = credential,
    ApplicationName = "my-app-name",
);

此示例需要两个 NuGet 包:

Google.Apis.Vision.v1  
Google.Apis.Oauth2.v2

【讨论】:

这里的主要焦点应该是 GoogleCredential.FromStream 其次是 CreateScoped【参考方案2】:

我设法获得了一个服务帐户来处理 P12 文件,但想知道如何使用 JSON 文件,或者只是从 JSON 文件中获取值来创建证书。

获取令牌

    private static String GetOAuthCredentialViaP12Key()
    
        const string serviceAccountEmail = SERVICE_ACCOUNT_EMAIL;
        var certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);

        var scope = DriveService.Scope.Drive + " https://spreadsheets.google.com/feeds";
        var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail)
                                                       
                                                            Scopes  = new[]  scope 
                                                       .FromCertificate(certificate) );

        if (credential.RequestAccessTokenAsync(CancellationToken.None).Result == false)
        

            return null;
        

        return credential.Token.AccessToken;
    

这就是我使用我得到的令牌的方式

        // Initialize the variables needed to make the request
        OAuth2Parameters parameters = new OAuth2Parameters AccessToken = token;
        GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
        SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
        service.RequestFactory = requestFactory;

【讨论】:

什么是 DriveService.Scope.Drive ?命名空间,引用? 我也在寻找一种使用 JSON 文件实现的方法。 SDK 版本 1.9.3 及更高版本支持此功能。请参阅:link 以下是如何使用它的快速示例: var stream = new FileStream(jsonPath, FileMode.Open, FileAccess.Read); GoogleCredential 凭证 = GoogleCredential.FromStream(stream); credential = credential.CreateScoped(new[] AnalyticsService.Scope.AnalyticsReadonly );【参考方案3】:

不是 C# 专家,但您似乎试图使用服务帐户来执行 OAuth2 Web 服务器流程,这应该不起作用。 您可能想改用ServiceAccountCredential。 有关不同 Google OAuth2 流程的更多信息,请参阅web server、service account 等文档。

【讨论】:

404。这就是不回答 SO 问题的方法 Service accounts will only work with Gsuite email accounts. The admin of the Gsuite (domain) account will have to grant the service account access to the users email account. Unfortunately i don't have access to a Gsuite account so cant help you set it up. This might help Perform G Suite Domain-Wide Delegation of Authority If this is a normal Gmail user account you are trying to access then you cant use a service account. You will either have to go though the SMTP / IMAP servers or using Oauth2 to authenticate the user.

以上是关于Google Oauth 错误:应设置至少一个客户端机密(已安装或 Web)的主要内容,如果未能解决你的问题,请参考以下文章

在按钮上单击列表视图中列的可见性应设置为 false

如何修复google OAuth2错误?

不是来自 Google API Oauth 的客户端的有效来源

带有 Google 的 OAuth2 - CORS 错误(Angular + Spring boot)[重复]

Google OAuth 2.0“错误”:“redirect_uri_mismatch”

AWS 认知;命中 /oauth2/token 时出现未授权客户端错误