鉴定为谷歌API和OAuth2 [英] Authenticating to Google API with OAuth2

查看:199
本文介绍了鉴定为谷歌API和OAuth2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出来的样品code从执行局使用C#/。NET和,像其他一些样品中我都试过了,的code,创建一个使用身份验证变量中的对象的一部分,说他们的语法是错误的。这里是code,我有:

I am trying out the sample code from Perform Google Apps Domain-wide Delegation of Authority using C# / .NET and, like in some other samples I have tried, the part of the code that creates an object that uses the auth variable says that they syntax is wrong. Here is the code I have:

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

这是我第一次写一个使用谷歌的API和任何帮助,得到这个工作将是极大的AP preciated!一个应用程序

This is my first time writing an app that uses Google's APIs and any help getting this working would be greatly appreciated!

推荐答案

这一件作品:

        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
        });

这篇关于鉴定为谷歌API和OAuth2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆