1

我正在尝试使来自https://developers.google.com/drive/service-accounts的 Google Drive 示例的 OAuth 2.0 服务帐户在 PowerShell 2.0 中工作。

$a= Add-Type -Path "D:\Google\OAuth\System.Web.Mvc.dll" -passthru
$b= Add-Type -Path "D:\Google\OAuth\DotNetOpenAuth.dll" -passthru
$c= Add-Type -Path "D:\Google\OAuth\Google.Apis.Authentication.OAuth2.dll" -passthru
$d= Add-Type -Path "D:\Google\OAuth\Newtonsoft.Json.Net35.dll" -passthru
$e= Add-Type -Path "D:\Google\OAuth\Google.Apis.dll" -passthru

$SERVICE_ACCOUNT_EMAIL = "<my service account email>"
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = "<path to my certl>"
$certificate = new-object System.Security.Cryptography.X509Certificates.X509Certificate2($SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable);

$desc= [Google.Apis.Authentication.OAuth2.GoogleAuthenticationServer]::Description

$provider = new-Object     Google.Apis.Authentication.OAuth2.DotNetOpenAuth.AssertionFlowClient($desc, $certificate)
$provider.ServiceAccountId = $SERVICE_ACCOUNT_EMAIL;
$provider.Scope = "https://www.google.com/m8/feeds"

$func= [Google.Apis.Authentication.OAuth2.DotNetOpenAuth.NativeApplicationClient]::AuthorizeRequest

$auth = new-Object "Google.Apis.Authentication.OAuth2.OAuth2Authenticator``1[Google.Apis.Authentication.OAuth2.DotNetOpenAuth.AssertionFlowClient]"($provider, $func);

不幸的是,最后一个新对象会导致以下错误消息:

New-Object : Cannot convert argument "1", with value: "static System.Void AuthorizeRequest(System.Net.HttpWebRequest re
quest, string accessToken)", for "OAuth2Authenticator`1" to type "System.Func`2[Google.Apis.Authentication.OAuth2.DotNe
tOpenAuth.AssertionFlowClient,DotNetOpenAuth.OAuth2.IAuthorizationState]": "Cannot convert the "static System.Void Auth
orizeRequest(System.Net.HttpWebRequest request, string accessToken)" value of type "System.Management.Automation.PSMeth
od" to type "System.Func`2[Google.Apis.Authentication.OAuth2.DotNetOpenAuth.AssertionFlowClient,DotNetOpenAuth.OAuth2.I
AuthorizationState]"."
At line:1 char:19
+ $auth = new-Object <<<<  "Google.Apis.Authentication.OAuth2.OAuth2Authenticator``1[Google.Apis.Authentication.OAuth2.
DotNetOpenAuth.AssertionFlowClient]"($provider, $func);
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

尝试处理传递该函数时似乎令人窒息。

有谁知道如何用这样的泛型做这种魔法?

4

1 回答 1

0

它抱怨 $func 的类型与预期的类型不匹配:

System.Func`2[Google.Apis.Authentication.OAuth2.DotNetOpenAuth.AssertionFlowClient,DotNetOpenAuth.OAuth2.IAuthorizationState]

或者在 C# 中说:

Func<AssertionFlowClient, IAuthorizationState>

也就是说,该函数必须接受一个 AssertionFlowClient 并返回一个实现 IAuthorizationState 的对象。你确定你是如何创建 $func 的吗?我在文档页面上没有看到静态 AuthorizeRequest 。

于 2013-08-23T23:24:46.403 回答