私は、同僚のプロジェクトでGoogle OAuth 2の例をC#からVb.netに翻訳しようとしていました。 私は、次の方法を翻訳する問題の端に抱えている:Google OAuthで使用するC#からvb.netへのデリゲート関数の翻訳に関する問題2
private OAuth2Authenticator<WebServerClient> CreateAuthenticator()
{
// Register the authenticator.
var provider = new WebServerClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = ClientCredentials.ClientID;
provider.ClientSecret = ClientCredentials.ClientSecret;
var authenticator =
new OAuth2Authenticator<WebServerClient>(provider, GetAuthorization) { NoCaching = true };
return authenticator;
}
private IAuthorizationState GetAuthorization(WebServerClient client)
{
// If this user is already authenticated, then just return the auth state.
IAuthorizationState state = AuthState;
if (state != null)
{
return state;
}
// Check if an authorization request already is in progress.
state = client.ProcessUserAuthorization(new HttpRequestInfo(HttpContext.Current.Request));
if (state != null && (!string.IsNullOrEmpty(state.AccessToken) || !string.IsNullOrEmpty(state.RefreshToken)))
{
// Store and return the credentials.
HttpContext.Current.Session["AUTH_STATE"] = _state = state;
return state;
}
// Otherwise do a new authorization request.
string scope = TasksService.Scopes.TasksReadonly.GetStringValue();
OutgoingWebResponse response = client.PrepareRequestUserAuthorization(new[] { scope });
response.Send(); // Will throw a ThreadAbortException to prevent sending another response.
return null;
}
主な問題は、この行であること:次のように
var authenticator = new OAuth2Authenticator<WebServerClient>(provider, GetAuthorization) { NoCaching = true };
メソッドのシグネチャは、この特定の行のよう読み込みが読み取ります
Public Sub New(tokenProvider As TClient, authProvider As System.Func(Of TClient, DotNetOpenAuth.OAuth2.IAuthorizationState))
VB.netのデリゲート関数についての私の理解は、最大ではありません。しかし、私はすべてのMSDNのドキュメントとWeb上の他の関連リソースを読んだことがありますが、私はまだこの特定の行をどのように翻訳するかについて固執しています。
これまでのところ、すべての試みで、キャストエラー(下記参照)またはGetAuthorizationの呼び出しが発生しました。
コード(vb.net .NET 3.5の)
Private Function CreateAuthenticator() As OAuth2Authenticator(Of WebServerClient)
' Register the authenticator.
' Register the authenticator.
Dim provider = New WebServerClient(GoogleAuthenticationServer.Description, oauth.ClientID, oauth.ClientSecret)
'GetAuthorization isn't called
'Dim authenticator = New OAuth2Authenticator(Of WebServerClient)(provider, AddressOf GetAuthorization) With {.NoCaching = True}
'This works, but results in type error
Dim authDelegate As Func(Of WebServerClient, IAuthorizationState) = AddressOf GetAuthorization
Dim authenticator = New OAuth2Authenticator(Of WebServerClient)(provider, authDelegate) With {.NoCaching = True}
'This works, but results in type error
'Dim authenticator = New OAuth2Authenticator(Of WebServerClient)(provider, GetAuthorization(provider)) With {.NoCaching = True}
'GetAuthorization isn't called
'Dim authenticator = New OAuth2Authenticator(Of WebServerClient)(provider, New Func(Of WebServerClient, IAuthorizationState)(Function(c) GetAuthorization(c))) With {.NoCaching = True}
'Dim authenticator = New OAuth2Authenticator(Of WebServerClient)(provider, New Func(Of WebServerClient, IAuthorizationState)(AddressOf GetAuthorization)) With {.NoCaching = True}
Return authenticator
End Function
Private Function GetAuthorization(arg As WebServerClient) As IAuthorizationState
' If this user is already authenticated, then just return the auth state.
Dim state As IAuthorizationState = AuthState
If (Not state Is Nothing) Then
Return state
End If
' Check if an authorization request already is in progress.
state = arg.ProcessUserAuthorization(New HttpRequestInfo(HttpContext.Current.Request))
If (state IsNot Nothing) Then
If ((String.IsNullOrEmpty(state.AccessToken) = False Or String.IsNullOrEmpty(state.RefreshToken) = False)) Then
' Store Credentials
HttpContext.Current.Session("AUTH_STATE") = state
_state = state
Return state
End If
End If
' Otherwise do a new authorization request.
Dim scope As String = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue()
Dim _response As OutgoingWebResponse = arg.PrepareRequestUserAuthorization(New String() {scope})
' Add Offline Access and forced Approval
_response.Headers("location") += "&access_type=offline&approval_prompt=force"
_response.Send() ' Will throw a ThreadAbortException to prevent sending another response.
Return Nothing
End Function
キャストエラー、私はこの上の一日の大半を費やしてきた
Server Error in '/' Application.
Unable to cast object of type 'DotNetOpenAuth.OAuth2.AuthorizationState' to type 'System.Func`2[DotNetOpenAuth.OAuth2.WebServerClient,DotNetOpenAuth.OAuth2.IAuthorizationState]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'DotNetOpenAuth.OAuth2.AuthorizationState' to type 'System.Func`2[DotNetOpenAuth.OAuth2.WebServerClient,DotNetOpenAuth.OAuth2.IAuthorizationState]'.
、および私はナッツを運転し始めている。 ヘルプは大歓迎です。私は、私はVB.netコード変換に利用できるオンラインのC#のすべてを試してみましたことを言及しなかったんだ
UPDATE
。問題の行の次の変換のすべての結果:
呼び出されていないGetAuthorization方法になりDim authenticator = New OAuth2Authenticator(Of WebServerClient)(provider, AddressOf GetAuthorization) With {.NoCaching = True}
。
私は同じ問題に直面しているので、この問題を解決できましたか? – Varun