【问题标题】:Authenticating to Google API with OAuth2使用 OAuth2 对 Google API 进行身份验证
【发布时间】:2013-06-24 23:57:32
【问题描述】:

我正在使用 C# / .NET 尝试来自 Perform Google Apps Domain-wide Delegation of Authority 的示例代码,并且与我尝试过的其他一些示例一样,创建使用 auth 变量的对象的代码部分表明它们的语法是错误的.这是我的代码:

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Drive.v2;
using Google.Apis.Util;

namespace GoogleAPIDemo
{
    class DriveServiceObject
    {
        private const string SERVICE_ACCOUNT_EMAIL = "<some-id>@developer.gserviceaccount.com";
        private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"\path\to\<public_key_fingerprint>-privatekey.p12";

        /// <summary>
        /// Build a Drive service object authorized with the service account
        /// that acts on behalf of the given user.
        /// </summary>
        /// @param userEmail The email of the user.
        /// <returns>Drive service object.</returns>
        static DriveService BuildService(String userEmail)
        {
            X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret",
                X509KeyStorageFlags.Exportable);

            var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
            {
                ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
                Scope = DriveService.Scopes.Drive.GetStringValue(),
                ServiceAccountUser = userEmail,
            };
            var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);

            return new DriveService(auth);
        }
    }
}

我看到的错误是

(local variable) OAuth2Athenticator<AssertionFlowClient> auth

Error:
   The best overloaded method match for 'Google.Apis.DriveService.DriveService(Googel.Apis.Services.BaseClientService.Initializer)' has some invalid arguments

这是我第一次编写使用 Google 的 API 的应用程序,我们将不胜感激!

【问题讨论】:

  • 'Google.Apis.DriveService.DriveService 的预期参数是什么,因为它不是 OAuth2Authenticator&lt;AssertionFlowClient 对象。

标签: c# .net google-drive-api google-admin-sdk


【解决方案1】:

这个有效:

        var provider = new AssertionFlowClient(
            GoogleAuthenticationServer.Description,
            new X509Certificate2(privateKeyPath, keyPassword, X509KeyStorageFlags.Exportable))
        {
            ServiceAccountId = serviceAccountEmail,
            Scope = DriveService.Scopes.Drive.GetStringValue(),
            ServiceAccountUser = driveHolderAccountEmail
        };
        var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);

        m_service = new DriveService(new BaseClientService.Initializer()
        {
            Authenticator = auth
        });

【讨论】:

  • 现在试试...很快就会回复。谢谢!
  • 这里的 m_service 和 keyPassword 是什么?它给出的错误类似于(当前上下文中不存在名称 'm_service')。请帮忙。
  • 默认keyPassword是“notasecret”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 2021-01-16
  • 2022-11-03
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多